获取当前时间(date+time),日期
NOW(), CURRENT_TIMESTAMP(),SYSDATE() 都可以获取当前时间。
NOW(), CURRENT_TIMESTAMP() 作用相同,获取的是执行sql语句时的时间。
SYSDATE() 获取的是执行到当前函数时的时间。
select NOW(), CURRENT_TIMESTAMP(),SYSDATE();
运行结果:
虽然 NOW(), CURRENT_TIMESTAMP(),SYSDATE() 结果是不是一模一样,但3者还是有区别的。运行以下sql
select NOW(), CURRENT_TIMESTAMP(),SYSDATE(),
SLEEP(10),
NOW(), CURRENT_TIMESTAMP(),SYSDATE();
运行结果
SLEEP 10 秒后,可以看到 NOW(), CURRENT_TIMESTAMP() 2次的时间一样,SYSDATE() 第二次获取的时间大于第一次10秒。产生差异的原因是SYSDATE函数返回的是执行到当前函数时的时间,而NOW(), CURRENT_TIMESTAMP()则返回的是执行sql语句时的时间。
时间转字符串
date_format,time_format 时间转字符串函数 。
date_format 对年月日和时分秒都支持。time_format 只支持时分秒,不支持年月日。
format的具体使用方法请看 MySQL DATE_FORMAT() 函数
select date_format(now(),'%Y-%m-%d %H:%i:%s'),
date_format(now(),'%Y-%m-%d'),
date_format(now(),'%H:%i:%s');
运行结果
select time_format(now(),'%Y-%m-%d %H:%i:%s'),
time_format(now(),'%Y-%m-%d'),
time_format(now(),'%H:%i:%s');
运行结果
字符串转时间
str_to_date(时间字符串,format格式) :字符串转时间
format格式请看 MySQL DATE_FORMAT() 函数
select str_to_date('2020-12-10 15:02:14','%Y-%m-%d %H:%i:%s'),
str_to_date('2020-12-10','%Y-%m-%d'),
str_to_date('15:02:14','%H:%i:%s');
执行结果
时间加,减
date_add(时间, interval n 时间单位) :给时间 + n 时间单位。支持 day,hour,minute,second,microsecond,week,month,quarter,year。
date_sub(时间, interval n 时间单位) :给时间 - n 时间单位。支持 day,hour,minute,second,microsecond,week,month,quarter,year。
set @dt = now();
select @dt;
select date_add(@dt, interval 1 day);
select date_add(@dt, interval 1 hour);
select date_add(@dt, interval 1 minute);
select date_add(@dt, interval 1 second);
select date_add(@dt, interval 1 microsecond);
select date_add(@dt, interval 1 week);
select date_add(@dt, interval 1 month);
select date_add(@dt, interval 1 quarter);
select date_add(@dt, interval 1 year);
select date_add(@dt, interval -1 day);
运行结果:
2020-12-10 15:58:45
2020-12-11 15:58:45
2020-12-10 16:58:45
2020-12-10 15:59:45
2020-12-10 15:58:46
2020-12-10 15:58:45.000001
2020-12-17 15:58:45
2021-01-10 15:58:45
2021-03-10 15:58:45
2021-12-10 15:58:45
2020-12-09 16:01:05
select @dt;
select date_sub(@dt, interval 1 day);
select date_sub(@dt, interval 1 hour);
select date_sub(@dt, interval 1 minute);
select date_sub(@dt, interval 1 second);
select date_sub(@dt, interval 1 microsecond);
select date_sub(@dt, interval 1 week);
select date_sub(@dt, interval 1 month);
select date_sub(@dt, interval 1 quarter);
select date_sub(@dt, interval 1 year);
select date_sub(@dt, interval -1 day);
运行结果:
2020-12-10 16:02:28
2020-12-09 16:02:28
2020-12-10 15:02:28
2020-12-10 16:01:28
2020-12-10 16:02:27
2020-12-10 16:02:27.999999
2020-12-03 16:02:28
2020-11-10 16:02:28
2020-09-10 16:02:28
2019-12-10 16:02:28
2020-12-11 16:02:28
计算时间间隔
timestampdiff(时间单位,小时间,大时间) :返回 大时间 - 小时间 的时间间隔。时间单位 支持:day,hour,minute,second,microsecond,week,month,quarter,year。
set @dt = now();
set @dt2 = date_add(@dt, interval 1 year);
select timestampdiff(day,@dt,@dt2);
select timestampdiff(hour,@dt,@dt2);
select timestampdiff(minute,@dt,@dt2);
select timestampdiff(second,@dt,@dt2);
select timestampdiff(microsecond,@dt,@dt2);
select timestampdiff(week,@dt,@dt2);
select timestampdiff(month,@dt,@dt2);
select timestampdiff(quarter,@dt,@dt2);
select timestampdiff(year,@dt,@dt2);
运行结果:
365
8760
525600
31536000
31536000000000
52
12
4
1