获取当前日期的函数
CURDATE() 、CURRENT_DATE()
例1:获取系统当前日期
1. mysql> select curdate(); 2. +------------+ 3. | curdate() | 4. +------------+ 5. | 2023-02-14 | 6. +------------+ 7. 1 row in set (0.00 sec)
例2:获取系统当前日期
1. mysql> select current_date(); 2. +----------------+ 3. | current_date() | 4. +----------------+ 5. | 2023-02-14 | 6. +----------------+ 7. 1 row in set (0.00 sec)
获取当前时间的函数
CURTIME() 、CURRENT_TIME()
例:获取系统当前时间,两个函数作用相同
1. mysql> select curtime(); 2. +-----------+ 3. | curtime() | 4. +-----------+ 5. | 16:15:16 | 6. +-----------+ 7. 1 row in set (0.00 sec) 8. 9. mysql> select current_time(); 10. +----------------+ 11. | current_time() | 12. +----------------+ 13. | 16:15:23 | 14. +----------------+ 15. 1 row in set (0.00 sec)
获取当前日期和时间的函数
CURRENT_TIMESTAMP() 、LOCALTIME() 、NOW() 、SYSDATE()
例:获取系统当前日期和时间,四个函数作用相同
1. mysql> select current_timestamp(); 2. +---------------------+ 3. | current_timestamp() | 4. +---------------------+ 5. | 2023-02-14 16:16:23 | 6. +---------------------+ 7. 1 row in set (0.00 sec) 8. 9. mysql> select localtime(); 10. +---------------------+ 11. | localtime() | 12. +---------------------+ 13. | 2023-02-14 16:17:49 | 14. +---------------------+ 15. 1 row in set (0.00 sec) 16. 17. mysql> select now(); 18. +---------------------+ 19. | now() | 20. +---------------------+ 21. | 2023-02-14 16:17:53 | 22. +---------------------+ 23. 1 row in set (0.00 sec) 24. 25. mysql> select sysdate(); 26. +---------------------+ 27. | sysdate() | 28. +---------------------+ 29. | 2023-02-14 16:18:01 | 30. +---------------------+ 31. 1 row in set (0.00 sec)
获取时间戳的函数
UNIX_TIMESTAMP()
例:获取系统当前时间戳
1. mysql> select unix_timestamp(); 2. +------------------+ 3. | unix_timestamp() | 4. +------------------+ 5. | 1676362724 | 6. +------------------+ 7. 1 row in set (0.00 sec)
转换时间戳的函数
FROM_UNIXTIME()
例:将时间戳转换为日期时间
1. mysql> select from_unixtime(1676362724); 2. +---------------------------+ 3. | from_unixtime(1676362724) | 4. +---------------------------+ 5. | 2023-02-14 16:18:44 | 6. +---------------------------+ 7. 1 row in set (0.00 sec)
获取 UTC 日期的函数
UTC_DATE()
例:获取当前 UTC (世界标准时间) 日期
1. mysql> select utc_date(); 2. +------------+ 3. | utc_date() | 4. +------------+ 5. | 2023-02-14 | 6. +------------+ 7. 1 row in set (0.00 sec)
获取 UTC 时间的函数
UTC_TIME()
例:获取当前UTC(世界标准时间)时间
1. mysql> select utc_time(); 2. +------------+ 3. | utc_time() | 4. +------------+ 5. | 08:22:05 | 6. +------------+ 7. 1 row in set (0.00 sec)
获取月份的函数
MONTH(date) 、MONTHNAME(date)
例:返回date对应的月份,返回结果为2,表示为2月
1. mysql> select month('2023-02-14'); 2. +---------------------+ 3. | month('2023-02-14') | 4. +---------------------+ 5. | 2 | 6. +---------------------+ 7. 1 row in set (0.00 sec)
例2:返回date对应月份,返回结果为february,英文表示2月
1. mysql> select monthname('2023-02-14'); 2. +-------------------------+ 3. | monthname('2023-02-14') | 4. +-------------------------+ 5. | February | 6. +-------------------------+ 7. 1 row in set (0.00 sec)
获取星期的函数
DAYNAME(date) 、DAYOFWEEK(date) 、WEEKDAY(date) 、WEEK(date) 、WEEKOFYEAR(date)
例1:返回date对应的工作日英文名称
1. mysql> select dayname('2023-02-14'); 2. +-----------------------+ 3. | dayname('2023-02-14') | 4. +-----------------------+ 5. | Tuesday | 6. +-----------------------+ 7. 1 row in set (0.00 sec)
例2:返回date对应一周的索引,1表示周日,2表示周一,...,7表示周六
1. mysql> select dayofweek('2023-02-14'); 2. +-------------------------+ 3. | dayofweek('2023-02-14') | 4. +-------------------------+ 5. | 3 | 6. +-------------------------+ 7. 1 row in set (0.00 sec)
例3:返回date对应一周的索引,0表示周一,1表示周二,...,6表示周日
1. mysql> select weekday('2023-02-14'); 2. +-----------------------+ 3. | weekday('2023-02-14') | 4. +-----------------------+ 5. | 1 | 6. +-----------------------+ 7. 1 row in set (0.00 sec)
例4:返回date是一年中的第几周,一年有52.1428571429周
1. mysql> select week('2023-02-14'); 2. +--------------------+ 3. | week('2023-02-14') | 4. +--------------------+ 5. | 7 | 6. +--------------------+ 7. 1 row in set (0.02 sec)
例5:返回date是一个月中的第几天
1. mysql> select dayofmonth('2023-02-14'); 2. +--------------------------+ 3. | dayofmonth('2023-02-14') | 4. +--------------------------+ 5. | 14 | 6. +--------------------------+ 7. 1 row in set (0.03 sec)
获取年份的函数
YEAR(date)
例:返回date对应的年份,如没有则返回null
1. mysql> select year('11-02-03'),year('90-09-39'); 2. +------------------+------------------+ 3. | year('11-02-03') | year('90-09-39') | 4. +------------------+------------------+ 5. | 2011 | NULL | 6. +------------------+------------------+ 7. 1 row in set, 1 warning (0.00 sec)
获取季度的函数
QUARTER(date)
例:返回date对应的季度,一年四个季度
1. mysql> select quarter('11-02-03'),quarter('11-04-03'); 2. +---------------------+---------------------+ 3. | quarter('11-02-03') | quarter('11-04-03') | 4. +---------------------+---------------------+ 5. | 1 | 2 | 6. +---------------------+---------------------+ 7. 1 row in set (0.00 sec)
获取分钟的函数
MINUTE(time)
例:返回当前时间的分钟,”now()”为当前时间
1. mysql> select minute(now()); 2. +---------------+ 3. | minute(now()) | 4. +---------------+ 5. | 48 | 6. +---------------+ 7. 1 row in set (0.00 sec)
获取秒钟的函数
SECOND(time)
例:返回当前时间的秒数
1. mysql> select second(now()); 2. +---------------+ 3. | second(now()) | 4. +---------------+ 5. | 24 | 6. +---------------+ 7. 1 row in set (0.00 sec)
获取日期的指定值的函数
EXTRACT(type FROM date)
例:分别返回年、月、日,type为日期类型,date为时间
1. mysql> select extract(year from now()); 2. +--------------------------+ 3. | extract(year from now()) | 4. +--------------------------+ 5. | 2023 | 6. +--------------------------+ 7. 1 row in set (0.00 sec) 8. 9. mysql> select extract(month from now()); 10. +---------------------------+ 11. | extract(month from now()) | 12. +---------------------------+ 13. | 2 | 14. +---------------------------+ 15. 1 row in set (0.00 sec) 16. 17. mysql> select extract(day from now()); 18. +-------------------------+ 19. | extract(day from now()) | 20. +-------------------------+ 21. | 15 | 22. +-------------------------+ 23. 1 row in set (0.00 sec)
时间和秒钟转换的函数
TIME_TO_SEC(time) 、SEC_TO_TIME(time)
例1:返回从零点到当前时间的秒数
1. mysql> select time_to_sec(now()); 2. +--------------------+ 3. | time_to_sec(now()) | 4. +--------------------+ 5. | 42959 | 6. +--------------------+ 7. 1 row in set (0.00 sec)
例2:把秒转换为时分秒
1. mysql> select sec_to_time(42959); 2. +--------------------+ 3. | sec_to_time(42959) | 4. +--------------------+ 5. | 11:55:59 | 6. +--------------------+ 7. 1 row in set (0.00 sec)
计算日期和时间的函数
DATE_ADD() 、ADDDATE() 、DATE_SUB() 、SUBDATE() 、ADDTIME() 、SUBTIME() 、DATEDIFF()
例1:DATE_ADD() 用于对日期进行加法运算,对指定的时间增加1分1秒
1. mysql> select date_add('2022-12-31 23:59:59',interval '1:1' minute_second); 2. +--------------------------------------------------------------+ 3. | date_add('2022-12-31 23:59:59',interval '1:1' minute_second) | 4. +--------------------------------------------------------------+ 5. | 2023-01-01 00:01:00 | 6. +--------------------------------------------------------------+ 7. 1 row in set (0.00 sec)
例2:ADDDATE()同上,用于对日期的加法运算
1. mysql> select adddate('2022-12-31 23:59:59',interval '1:1' minute_second); 2. +-------------------------------------------------------------+ 3. | adddate('2022-12-31 23:59:59',interval '1:1' minute_second) | 4. +-------------------------------------------------------------+ 5. | 2023-01-01 00:01:00 | 6. +-------------------------------------------------------------+ 7. 1 row in set (0.01 sec)
ADDDATE()也可以通过下面语法添加天数,格式如下
1. mysql> select adddate('2023-01-01',10); 2. +--------------------------+ 3. | adddate('2023-01-01',10) | 4. +--------------------------+ 5. | 2023-01-11 | 6. +--------------------------+ 7. 1 row in set (0.00 sec)
例3:DATE_SUB()用于对日期的减法运算,如下:2月1日减去31天
1. mysql> select date_sub('2023-02-01',interval 31 day); 2. +----------------------------------------+ 3. | date_sub('2023-02-01',interval 31 day) | 4. +----------------------------------------+ 5. | 2023-01-01 | 6. +----------------------------------------+ 7. 1 row in set (0.00 sec)
例4:SUBDATE()同上,对日期进行减法运算
1. mysql> select subdate('2023-02-01',interval 31 day); 2. +---------------------------------------+ 3. | subdate('2023-02-01',interval 31 day) | 4. +---------------------------------------+ 5. | 2023-01-01 | 6. +---------------------------------------+ 7. 1 row in set (0.00 sec)
SUBDATE()也可以通过下面语法减少天数,格式如下
1. mysql> select subdate('2023-02-01',31); 2. +--------------------------+ 3. | subdate('2023-02-01',31) | 4. +--------------------------+ 5. | 2023-01-01 | 6. +--------------------------+
例5:ADDTIME()对日期进行加法运算,给指定的日期增加1时1分1秒
1. mysql> select addtime('2023-12-30 23:59:59','1:1:1'); 2. +----------------------------------------+ 3. | addtime('2023-12-30 23:59:59','1:1:1') | 4. +----------------------------------------+ 5. | 2023-12-31 01:01:00 | 6. +----------------------------------------+ 7. 1 row in set (0.00 sec)
例6:SUBTIME() 对日期进行减法运算,给指定的日期减去1时1分1秒
1. mysql> select subtime('2023-12-30 23:59:59','1:1:1'); 2. +----------------------------------------+ 3. | subtime('2023-12-30 23:59:59','1:1:1') | 4. +----------------------------------------+ 5. | 2023-12-30 22:58:58 | 6. +----------------------------------------+ 7. 1 row in set (0.00 sec)
例7:DATEDIFF() 用于计算两个日期之间的间隔天数
1. mysql> select datediff('2023-01-31','2023-01-1'); 2. +------------------------------------+ 3. | datediff('2023-01-31','2023-01-1') | 4. +------------------------------------+ 5. | 30 | 6. +------------------------------------+ 7. 1 row in set (0.00 sec)
将日期和时间格式化的函数
DATE_FORMAT(date, format) 、TIME_FORMAT(time, format) 、GET_FORMAT(val_type, format_type)
格式化类型在下面,根据需求修改
例1:DATE_FORMAT(date, format)用于格式化日期,根据format指定的格式显示date值
1. mysql> select date_format('2023-01-31 22:30:00','%W %M %Y'); 2. +-----------------------------------------------+ 3. | date_format('2023-01-31 22:30:00','%W %M %Y') | 4. +-----------------------------------------------+ 5. | Tuesday January 2023 | 6. +-----------------------------------------------+ 7. 1 row in set (0.01 sec)
例2:TIME_FORMAT(time, format) 格式化时间,根据format指定的格式显示time值
1. mysql> select time_format('16:00:00','%H %k %I'); 2. +------------------------------------+ 3. | time_format('16:00:00','%H %k %I') | 4. +------------------------------------+ 5. | 16 16 04 | 6. +------------------------------------+ 7. 1 row in set (0.00 sec)
例3:GET_FORMAT(val_type, format_type)指定值的类型和格式化类型,然后将其转换为字符串
1. mysql> select date_format('2023-01-20 22:30:00',get_format(date,'usa')); 2. +-----------------------------------------------------------+ 3. | date_format('2023-01-20 22:30:00',get_format(date,'usa')) | 4. +-----------------------------------------------------------+ 5. | 01.20.2023 | 6. +-----------------------------------------------------------+ 7. 1 row in set (0.00 sec)
类型 |
说明 |
%d |
该月日期,数字形式(00..31) |
%e |
该月日期,数字形式(0..31) |
%f |
微秒(000000...999999) |
%H |
以2位数表示24小时(00..23) |
%h,%I |
以2位数表示12小时(01..12) |
%i |
分钟,数字形式(00-59) |
%j |
一年中的天数(001-366) |
%k |
以24小时(0-23) |
%l |
以12小时(0..12) |
%M |
月份名称(january..December) |
%m |
月份数字形式(00..12) |
%p |
上午(AM)或下午(PM) |
%r |
时间,12小时制(小时hh:分钟mm:秒钟ss后面加AM或PM) |
%S,%s |
以2位数形式表示秒(00..59) |
%T |
时间,24小时制(小时hh:分钟mm:秒数ss) |
%U |
周(00..53),其中周日为每周的第一天 %u周(00..53),其中周一为每周的第一天 |
%V |
周(01..53),其中周日为每周的第一天,和%X一起使用 |
%v |
周(01..53),其中周一为每周的第一天,和%x一起使用 |
%W |
工作日名称(周日..周六) |
%w |
一周中的每日(0=周日..6=周六) |
%X |
该周的年份,其中周日为每周的第一天;数字形式4位数,和%V同时使用 |
%x |
该周的年份,其中周一为每周的第一天;数字形式4位数,和%v同时使用 |
%Y |
4位数形式表示年份 |
%y |
2位数形式表示年份 |
%% |
“%”文字字符 |