日期时间函数

1、获取子值,值为整数类型,函数如下

year(date)返回date的年份(范围在1000到9999)
month(date)返回date中的月份数值
day(date)返回date中的日期数值
hour(time)返回time的小时数(范围是0到23)
minute(time)返回time的分钟数(范围是0到59)
second(time)返回time的秒数(范围是0到59)
select year('2016-12-21');

2、日期计算,使用+-运算符,数字后面的关键字为

year、month、day、hour、minute、second
select '2016-12-21'+interval 1 day;

3、日期格式化date_format(date,format)

参数format可选值如下

%Y 获取年,返回完整年份
%y 获取年,返回简写年份
%m 获取月,返回月份
%d 获取日,返回天值
%H 获取时,返回24进制的小时数
%h 获取时,返回12进制的小时数
%i 获取分,返回分钟数
%s 获取秒,返回秒数

示例如下:将使用-拼接的日期转换为使用空格拼接

select date_format('2016-12-21','%Y %m %d');

4、当前日期current_date()

select current_date();

5、当前时间current_time()

select current_time();

6、当前日期时间now()

select now();