参考mysql官网:https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html

1.改变日期格式

SELECT DATE_FORMAT(‘2007-10-04 22:23:00’, ‘%H:%i:%s’);
-> ‘22:23:00’

2.把timestamp变成日期

SELECT FROM_UNIXTIME(1447430881);
-> ‘2015-11-13 10:08:01’

3.把日期变成timestamp

SELECT UNIX_TIMESTAMP(‘2015-11-13 10:20:19.012’);
-> 1447431619.012

4.extract 月份

SELECT MONTH(‘2008-02-03’);
-> 2

5.extract 分钟

SELECT MINUTE(‘2008-02-03 10:05:03’);
-> 5

6.月份的第几天

SELECT DAYOFMONTH(‘2007-02-03’);
-> 3
day()=dayofmonth()
dayofyear()范围为(1-365)

7.周几

SELECT DAYOFWEEK(‘2007-02-03’);
-> 7

8.日期相减

SELECT DATEDIFF(‘2007-12-31 23:59:59’,’2007-12-30’);
-> 1

9.当前时间

SELECT CURTIME(); -> ‘23:50:26’

10.当前日期

SELECT CURDATE(); -> ‘2008-06-13’

11.当前日期+时间

SELECT NOW(); -> ‘2007-12-15 23:50:26’