- now 返回当前系统日期+时间
now 返回当前系统日期+时间
案例1:
select now()
结果:
2019-07-12 19:59:08
2. curdate 返回当前系统的日期,不包含时间
select curdate()
结果:
2019-07-12
3. curtime 返回当前系统的时间,不包含日期
案例1:
select curtime()
结果:
20:04:45
- 可以获取指定的部分,
年YEAR,
月month,英文月份MONTHNAME
日day,
小时hour,
分钟MINUTE,
秒SECOND
案例1:
select YEAR(NOW()),'从mow里面取到年' union all
select YEAR('1995-02-07'),'年' union all
select month('1995-02-07'),'月' union all
select MONTHNAME('1995-02-07'),'英文的月份' union all
SELECT day('1995-02-07'),'日' union all
select hour('2019-07-12 20:12:55'),'小时' union all
select MINUTE('2019-07-12 20:12:55') ,'分钟' union all
select SECOND('2019-07-12 20:12:55') ,'秒'
结果:
2019 从mow里面取到年
1995 年
2 月
February 英文的月份
7 日
20 小时
12 分钟
55 秒
- str_to_date
作用: 将日期格式的字符转换成指定格式的日期
str_to_date(‘给定的日期格式字符’,‘按照什么格式解析也就是对应关系’)
str_to_date(‘9-12-1999’,’%m-%d%-%Y’) 得到: 1999-09-13
格式符与功能的对应关系:
str_to_date作用:将日期格式的字符转换成指定格式的日期
案例1:
select STR_TO_DATE('1998-3-2 21:23:23','%Y-%c-%d %H:%i:%s'),'按照指定格式解析' as out_put union all
select STR_TO_DATE('2098-3-2 ','%y-%c-%d'),'年份是4位的,用了y格式解析错误,所以weinill' as out_put union all
select STR_TO_DATE('2098-3-2 ','%Y-%c-%d'),'格式正确'as out_put
结果:
1998-03-02 21:23:23 按照指定格式解析
null 年份是4位的,用了y格式解析错误,所以weinill
2098-03-02 00:00:00 格式正确
- date_format :将日期格式转换成字符
date_format (‘A’,‘B’) 将日期格式的A按照B的格式转换成字符类型
案例1:
select date_format(NOW(),'%y年%m月%d日') as out_put union all
select date_format(NOW(),'%m月%d日 %Y年') as out_put
结果:
19年07月12日
07月12日 2019年
课件里面的例子: