MySQL之日期时间函数
1.NOW()
用法:显示当前日期和时间
举例:
mysql> select NOW(); +---------------------+ | NOW() | +---------------------+ | 2014-11-05 21:16:21 | +---------------------+ 1 row in set (0.04 sec)
2.CURDATE(),CURTIME()
用法:显示当前日期()
举例:
mysql> select CURDATE(); +------------+ | CURDATE() | +------------+ | 2014-11-05 | +------------+ 1 row in set (0.00 sec) mysql> select CURTIME(); +-----------+ | CURTIME() | +-----------+ | 21:16:47 | +-----------+ 1 row in set (0.00 sec)
3.DATE_ADD()
用法:日期添加函数
举例:
mysql> select DATE_ADD('2014-11-5',INTERVAL 365 DAY); +----------------------------------------+ | DATE_ADD('2014-11-5',INTERVAL 365 DAY) | +----------------------------------------+ | 2015-11-05 | +----------------------------------------+ 1 row in set (0.00 sec) mysql> select DATE_ADD('2014-11-5',INTERVAL 3 YEAR); +---------------------------------------+ | DATE_ADD('2014-11-5',INTERVAL 3 YEAR) | +---------------------------------------+ | 2017-11-05 | +---------------------------------------+ 1 row in set (0.00 sec) mysql> select DATE_ADD('2014-11-5',INTERVAL 3 MONTH); +----------------------------------------+ | DATE_ADD('2014-11-5',INTERVAL 3 MONTH) | +----------------------------------------+ | 2015-02-05 | +----------------------------------------+ 1 row in set (0.00 sec)
4.DATEDIFF()
用法:日期比较
举例:
mysql> select DATEDIFF('2014-11-2','2014-11-23'); +------------------------------------+ | DATEDIFF('2014-11-2','2014-11-23') | +------------------------------------+ | -21 | +------------------------------------+ 1 row in set (0.04 sec) mysql> select DATEDIFF('2014-12-2','2014-11-23'); +------------------------------------+ | DATEDIFF('2014-12-2','2014-11-23') | +------------------------------------+ | 9 | +------------------------------------+ 1 row in set (0.00 sec)
5.DATE_FORMAT()
用法:日期格式化
DATE_FORMAT(date,format)
根据format字符串格式化date值
(在format字符串中可用标志符:
%M 月名字(January……December)
%W 星期名字(Sunday……Saturday)
%D 有英语前缀的月份的日期(1st, 2nd, 3rd, 等等。)
%Y 年, 数字, 4 位
%y 年, 数字, 2 位
%a 缩写的星期名字(Sun……Sat)
%d 月份中的天数, 数字(00……31)
%e 月份中的天数, 数字(0……31)
%m 月, 数字(01……12)
%c 月, 数字(1……12)
%b 缩写的月份名字(Jan……Dec)
%j 一年中的天数(001……366)
%H 小时(00……23)
%k 小时(0……23)
%h 小时(01……12)
%I 小时(01……12)
%l 小时(1……12)
%i 分钟, 数字(00……59)
%r 时间,12 小时(hh:mm:ss [AP]M)
%T 时间,24 小时(hh:mm:ss)
%S 秒(00……59)
%s 秒(00……59)
%p AM或PM
%w 一个星期中的天数(0=Sunday ……6=Saturday )
%U 星期(0……52), 这里星期天是星期的第一天
%u 星期(0……52), 这里星期一是星期的第一天
%% 字符% )
举例:
mysql> select DATE_FORMAT('2014-11-5','%Y/%M/%D'); +-------------------------------------+ | DATE_FORMAT('2014-11-5','%Y/%M/%D') | +-------------------------------------+ | 2014/November/5th | +-------------------------------------+ 1 row in set (0.03 sec)
6.DAYOFWEEK(date),WEEKDAY(date),DAYOFMONTH(date),DAYOFYEAR(date),MONTH(date),DAYNAME(date)
用法:返回相关的日期时间信息
举例:
mysql> select DAYOFWEEK('2014-11-05'); +-------------------------+ | DAYOFWEEK('2014-11-05') | +-------------------------+ | 4 | +-------------------------+ 1 row in set (0.00 sec) mysql> select WEEKDAY('2014-11-05'); +-----------------------+ | WEEKDAY('2014-11-05') | +-----------------------+ | 2 | +-----------------------+ 1 row in set (0.00 sec) mysql> select DAYOFMONTH('2014-11-05'); +--------------------------+ | DAYOFMONTH('2014-11-05') | +--------------------------+ | 5 | +--------------------------+ 1 row in set (0.00 sec) mysql> select DAYOFYEAR('2014-11-05'); +-------------------------+ | DAYOFYEAR('2014-11-05') | +-------------------------+ | 309 | +-------------------------+ 1 row in set (0.00 sec) mysql> select MONTH('2014-11-05'); +---------------------+ | MONTH('2014-11-05') | +---------------------+ | 11 | +---------------------+ 1 row in set (0.00 sec)
7.CURRENT_TIMESTAMP(),SYSDATE()
用法:返回当前的时间戳(以'YYYY-MM-DD HH:MM:SS'或YYYYMMDDHHMMSS格式返回当前日期时间)
举例:
mysql> select CURRENT_TIMESTAMP(); +---------------------+ | CURRENT_TIMESTAMP() | +---------------------+ | 2014-11-05 21:31:46 | +---------------------+ 1 row in set (0.00 sec) mysql> select SYSDATE(); +---------------------+ | SYSDATE() | +---------------------+ | 2014-11-05 21:32:00 | +---------------------+ 1 row in set (0.00 sec)