MySQL常用日期函数
函数 | 描述 |
---|---|
NOW() | 返回当前的日期和时间 |
CURDATE() | 返回当前的日期 |
CURTIME() | 返回当前的时间 |
DATE() | 提取日期或日期/时间表达式的日期部分 |
EXTRACT() | 返回日期/时间按的单独部分 |
DATE_ADD() | 给日期添加指定的时间间隔 |
DATE_SUB() | 从日期减去指定的时间间隔 |
DATEDIFF() | 返回两个日期之间的天数 |
DATE_FORMAT() | 用不同的格式显示日期/时间 |
1、获取当前日期时间
# 获取当前日期时间
SELECT NOW();
SELECT LOCALTIME();
SELECT LOCALTIME;
SELECT LOCALTIMESTAMP();
SELECT LOCALTIMESTAMP;
SELECT CURRENT_TIMESTAMP();
SELECT CURRENT_TIMESTAMP;
SELECT SYSDATE();
# 获取当前日期
SELECT CURDATE();
SELECT CURRENT_DATE();
SELECT CURRENT_DATE;
SELECT DATE(NOW());
# 获取当前时间
SELECT CURTIME();
SELECT CURRENT_TIME();
SELECT CURRENT_TIME;
SELECT TIME(NOW());
# 获取UTC日期时间
SELECT UTC_TIMESTAMP(), UTC_DATE(), UTC_TIME()
2、获取日期的年、月、日、时、分、秒
# 对于时间2022-05-20 08:30:16,分别获取其年、月、日、时、分、秒
SELECT EXTRACT(YEAR FROM NOW()); # 2022(年)
SELECT EXTRACT(MONTH FROM NOW()); # 5(月)
SELECT EXTRACT(DAY FROM NOW()); # 20(日)
SELECT EXTRACT(HOUR FROM NOW()); # 8(小时)
SELECT EXTRACT(MINUTE FROM NOW()); # 30(分钟)
SELECT EXTRACT(SECOND FROM NOW()); # 16(秒)
SELECT EXTRACT(MICROSECOND FROM NOW()); # 0(毫秒)
SELECT EXTRACT(QUARTER FROM NOW()); # 2(季度)
SELECT EXTRACT(WEEK FROM NOW()); # 20(日期在年度中第几周)
SELECT EXTRACT(SECOND FROM '2022-05-20 08:30:16'); # 16
SELECT YEAR(NOW()); # 2022(年)
SELECT MONTH(NOW()); # 5(月)
SELECT DAY(NOW()); # 20(日)
SELECT HOUR(NOW()); # 8(小时)
SELECT MINUTE(NOW()); # 30(分钟)
SELECT SECOND(NOW()); # 16(秒)
SELECT MICROSECOND(NOW()); # 0(毫秒)
SELECT QUARTER(NOW()); # 2(季度)
SELECT WEEK(NOW()); # 20(日期在年度中第几周)
SELECT DAYOFYEAR(NOW()); # 140(日期在年度中第几天)
SELECT DAYOFWEEK(NOW()); # 6(日期在周中第几天;周日为第1天)
SELECT WEEKDAY(NOW()); # 4(日期在周中第几天;周一为第0天)
SELECT DAYNAME(NOW()); # Friday(返回英文星期)
SELECT MONTHNAME(NOW()); # May(返回英文月份)
SELECT LAST_DAY(NOW()); # 2022-05-31(返回月份中最后一天)
3、日期格式化
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d %H:%i:%s');
SELECT DATE_FORMAT(NOW(), '%Y-%m-%d');
SELECT DATE_FORMAT(NOW(), '%H:%i:%s');
SELECT TIME_FORMAT(NOW(), '%H:%i:%s');
4、日期转换
# 字符串转日期
SELECT STR_TO_DATE('2022-05-20 08:30:16', '%Y-%m-%d %H:%i:%s');
# 时间转秒
SELECT TIME_TO_SEC('01:00:00'); # 3600
# 秒转时间
SELECT SEC_TO_TIME(3600); # 01:00:00
# 时间转时间戳
SELECT UNIX_TIMESTAMP(); # 1653046990
SELECT UNIX_TIMESTAMP('2022-05-20'); # 1652976000
SELECT UNIX_TIMESTAMP('2022-05-20 08:30:16'); # 1653006616
# 时间戳转时间
SELECT FROM_UNIXTIME(1653006616); # 2022-05-20 08:30:16
SELECT FROM_UNIXTIME(1653006616, '%Y-%m-%d %h:%i:%s'); # 2022-05-20 08:30:16
5、日期增加、减少
# 获取三十天后日期
SELECT DATE_SUB(CURDATE(), INTERVAL -30 DAY);
SELECT DATE_ADD(CURDATE(), INTERVAL 30 DAY);
SELECT TIMESTAMPADD(DAY, 30, CURDATE());
# 获取三十天前日期
SELECT DATE_SUB(CURDATE(), INTERVAL 30 DAY);
SELECT DATE_ADD(CURDATE(), INTERVAL -30 DAY);
SELECT TIMESTAMPADD(DAY, -30, CURDATE());
# 获取一周后日期
SELECT DATE_SUB(CURDATE(), INTERVAL -1 WEEK);
SELECT DATE_ADD(CURDATE(), INTERVAL 1 WEEK);
SELECT TIMESTAMPADD(WEEK, 1, CURDATE());
# 获取一月后日期
SELECT DATE_SUB(CURDATE(), INTERVAL -1 MONTH);
SELECT DATE_ADD(CURDATE(), INTERVAL 1 MONTH);
SELECT TIMESTAMPADD(MONTH, 1, CURDATE());
# 获取一年后日期
SELECT DATE_SUB(CURDATE(), INTERVAL -1 YEAR);
SELECT DATE_ADD(CURDATE(), INTERVAL 1 YEAR);
SELECT TIMESTAMPADD(YEAR, 1, CURDATE());
6、时区转换
SELECT CONVERT_TZ(NOW(), '+08:00', '+00:00');
7、获取时差
SELECT TIMESTAMPDIFF(YEAR, '2017-06-14 00:00:00', '2018-07-15 08:12:25');
SELECT TIMESTAMPDIFF(MONTH, '2017-06-14 00:00:00', '2018-07-15 08:12:25');
SELECT TIMESTAMPDIFF(DAY, '2017-06-14 00:00:00', '2018-07-15 08:12:25');
SELECT TIMESTAMPDIFF(WEEK, '2017-06-14 00:00:00', '2018-07-15 08:12:25');
SELECT TIMESTAMPDIFF(HOUR, '2017-06-14 00:00:00', '2018-07-15 08:12:25');
SELECT TIMESTAMPDIFF(MINUTE, '2017-06-14 00:00:00', '2018-07-15 08:12:25');
SELECT TIMESTAMPDIFF(SECOND, '2017-06-14 00:00:00', '2018-07-15 08:12:25');
8、获取日期范围的数据
# 获取未来的数据
select * from table1 where NOW()<=date(create_time);
# 获取未来三十天的数据(精确到秒)
select * from table1 where NOW()<=date(create_time) and date(create_time) <=DATE_SUB(CURDATE(),INTERVAL -30 DAY)
# 获取未来三十天的数据(精确到天)
select * from table1 WHERE TO_DAYS(NOW()) <= TO_DAYS(date(create_time)) AND TO_DAYS(date(create_time)) <= TO_DAYS(DATE_ADD(CURDATE(), INTERVAL 30 DAY));
# 获取当天数据
select * from table1 where TO_DAYS(create_time) = TO_DAYS(NOW());