博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
MySQL之日期函数和时间函数总结
阅读量:5746 次
发布时间:2019-06-18

本文共 10832 字,大约阅读时间需要 36 分钟。

日期函数、时间函数总结

一、获取mysql当前时间

获取当前日期+时间(date+time)函数:now();

mysql> select localtime();mysql> select now();+---------------------+| now()               |+---------------------+| 2018-09-16 19:26:55 |+---------------------+1 row in set (0.00 sec)mysql> select current_timestamp();+---------------------+| current_timestamp() |+---------------------+| 2018-09-16 19:27:21 |+---------------------+1 row in set (0.00 sec)

二、MySQL 日期转换函数和时间转换函数

MySQL Date/Time to Str(日期/时间转换为字符串)函数:

date_format(date,format), time_format(time,format)mysql> select date_format('2018-09-16','%Y/%m/%d');+--------------------------------------+| date_format('2018-09-16','%Y/%m/%d') |+--------------------------------------+| 2018/09/16                           |+--------------------------------------+1 row in set (0.00 sec)mysql> select date_format('2018-09-16 19:32:20','%Y%m%d%H%i%s');+---------------------------------------------------+| date_format('2018-09-16 19:32:20','%Y%m%d%H%i%s') |+---------------------------------------------------+| 20180916193220                                    |+---------------------------------------------------+1 row in set (0.00 sec)

提示:如果为大写M,则为英文9月,大写D则为英文16th,大写H则可以正常显示12点以后的数字

mysql> select date_format('2018-09-16 19:32:20','%Y%M%D%H%i%s');+---------------------------------------------------+| date_format('2018-09-16 19:32:20','%Y%M%D%H%i%s') |+---------------------------------------------------+| 2018September16th193220                           |+---------------------------------------------------+1 row in set (0.00 sec)mysql> select time_format('18:23:20','%H/%i/%s');+------------------------------------+| time_format('18:23:20','%H/%i/%s') |+------------------------------------+| 18/23/20                           |+------------------------------------+1 row in set (0.00 sec)mysql> select time_format('18:23:20','%H-%i-%s');+------------------------------------+| time_format('18:23:20','%H-%i-%s') |+------------------------------------+| 18-23-20                           |+------------------------------------+1 row in set (0.00 sec)

MySQL 日期、时间转换函数:

date_format(date,format), time_format(time,format) 能够把一个日期/时间转换成各种各样的字符串格式。它是 str_to_date(str,format) 函数的 一个逆转换

mysql> select str_to_date('16/09/2018', '%d/%m/%Y');+---------------------------------------+| str_to_date('16/09/2018', '%d/%m/%Y') |+---------------------------------------+| 2018-09-16                            |+---------------------------------------+1 row in set (0.00 sec)mysql> select str_to_date('16.09.2018', '%d.%m.%Y');+---------------------------------------+| str_to_date('16.09.2018', '%d.%m.%Y') |+---------------------------------------+| 2018-09-16                            |+---------------------------------------+1 row in set (0.00 sec)

MySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)

mysql> select time_to_sec('01:00:05');+-------------------------+| time_to_sec('01:00:05') |+-------------------------+|                    3605 |+-------------------------+1 row in set (0.00 sec)mysql> select sec_to_time('3605');+---------------------+| sec_to_time('3605') |+---------------------+| 01:00:05.000000     |+---------------------+1 row in set (0.02 sec)mysql>

MySQL 拼凑日期、时间函数:makedate(year,dayofyear), maketime(hour,minute,second)

mysql> select makedate(2018,31);+-------------------+| makedate(2018,31) |+-------------------+| 2018-01-31        |+-------------------+1 row in set (0.00 sec)mysql> select makedate(2018,32);+-------------------+| makedate(2018,32) |+-------------------+| 2018-02-01        |+-------------------+1 row in set (0.00 sec)mysql> select makedate(2018,259);+--------------------+| makedate(2018,259) |+--------------------+| 2018-09-16         |+--------------------+1 row in set (0.00 sec)mysql> mysql> select maketime(12,15,30);+--------------------+| maketime(12,15,30) |+--------------------+| 12:15:30           |+--------------------+1 row in set (0.00 sec)

MySQL (Unix 时间戳、日期)转换函数

unix_timestamp(),unix_timestamp(date),from_unixtime(unix_timestamp),from_unixtime(unix_timestamp,format)select unix_timestamp(); -- 1218290027select unix_timestamp('2008-08-08'); -- 1218124800select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800select from_unixtime(1218290027); -- '2008-08-09 21:53:47'select from_unixtime(1218124800); -- '2008-08-08 00:00:00'select from_unixtime(1218169800); -- '2008-08-08 12:30:00'select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'

三、MySQL 日期时间计算函数:

date_add() MySQL日期增加函数:

date_add(curdate(), interval 1 day); ##curdate()代表当前日期,interval + num代表增加num 天,interval - num代表减去num 天

mysql> select date_add(curdate(), interval 1 day);+-------------------------------------+| date_add(curdate(), interval 1 day) |+-------------------------------------+| 2018-09-17                          |+-------------------------------------+1 row in set (0.00 sec)

select date_add(curtime(), interval -1 hour); ##curtime()代表当前时间; interval -1 hour代表减去1小时。

mysql> select date_add(curtime(), interval -1 hour);+---------------------------------------+| date_add(curtime(), interval -1 hour) |+---------------------------------------+| 19:14:10                              |+---------------------------------------+1 row in set (0.00 sec)mysql> select date_add(curtime(), interval +1 hour);+---------------------------------------+| date_add(curtime(), interval +1 hour) |+---------------------------------------+| 21:14:31                              |+---------------------------------------+1 row in set (0.00 sec)

MySQL 为日期增加一个时间间隔:

set @dt = now();select date_add(@dt, interval 1 day); -- add 1 dayselect date_add(@dt, interval 1 hour); -- add 1 hourselect 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); -- sub 1 day

选取日期时间的各个部分:日期、时间、年、季度、月、日、小时、分钟、秒、微秒

set @dt = '20018-09-10 07:15:30.123456';select date(@dt); -- 20018-09-10select time(@dt); -- 07:15:30.123456select year(@dt); -- 2008select quarter(@dt); -- 3select month(@dt); -- 9select week(@dt); -- 36select day(@dt); -- 10select hour(@dt); -- 7select minute(@dt); -- 15select second(@dt); -- 30select microsecond(@dt); -- 123456

MySQL adddate(), addtime()函数,可以用 date_add() 来替代。下面是 date_add() 实现 addtime() 功能示例:

mysql> set @dt = '2018-09-16 20:12:33';mysql> select date_add(@dt, interval '01:15:30' hour_second);+------------------------------------------------+| date_add(@dt, interval '01:15:30' hour_second) |+------------------------------------------------+| 2018-09-16 21:28:03                            |+------------------------------------------------+1 row in set (0.00 sec)mysql> mysql>  select date_add(@dt, interval '1 01:15:30' day_second);+-------------------------------------------------+| date_add(@dt, interval '1 01:15:30' day_second) |+-------------------------------------------------+| 2018-09-17 21:28:03                             |+-------------------------------------------------+1 row in set (0.00 sec)mysql>

date_sub()mysql日期减去函数:

MySQL 为日期减去一个时间间隔:date_sub()
当前日期减去一天

mysql> select date_sub(curdate(),interval 1 day);+------------------------------------+| date_sub(curdate(),interval 1 day) |+------------------------------------+| 2018-09-15                         |+------------------------------------+1 row in set (0.00 sec)

当前日期增加一天:

mysql> select date_sub(curdate(),interval -1 day);+-------------------------------------+| date_sub(curdate(),interval -1 day) |+-------------------------------------+| 2018-09-17                          |+-------------------------------------+1 row in set (0.00 sec)

MySQL为日期2018-09-16 00:00:00减去一个时间间隔

mysql> select date_sub('2018-09-16 00:00:00', interval '1 1:1:1' day_second);+----------------------------------------------------------------+| date_sub('2018-09-16 00:00:00', interval '1 1:1:1' day_second) |+----------------------------------------------------------------+| 2018-09-14 22:58:59                                            |+----------------------------------------------------------------+1 row in set (0.00 sec)

提示:

MySQL date_sub() 日期时间函数 和 date_add() 用法一致,不再赘述。

MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)

MySQL datediff(date1,date2):两个日期相减 date1 - date2,返回天数。mysql> select datediff('2018-09-16','2018-08-16');+-------------------------------------+| datediff('2018-09-16','2018-08-16') |+-------------------------------------+|                                  31 |+-------------------------------------+1 row in set (0.00 sec)

MySQL timediff(time1,time2):两个日期相减 time1 - time2,返回 time 差值。

mysql> select timediff('20:49:30','20:48:10');+---------------------------------+| timediff('20:49:30','20:48:10') |+---------------------------------+| 00:01:20                        |+---------------------------------+1 row in set (0.00 sec)mysql> select timediff('2018-09-16 20:49:30','2018-09-15 20:48:10');+-------------------------------------------------------+| timediff('2018-09-16 20:49:30','2018-09-15 20:48:10') |+-------------------------------------------------------+| 24:01:20                                              |+-------------------------------------------------------+1 row in set (0.00 sec)

注意:timediff(time1,time2) 函数的两个参数类型必须相同。

四、MySQL 时间戳(timestamp)转换、增、减函数:

timestamp(date) -- date to timestamptimestamp(dt,time) -- dt + timetimestampadd(unit,interval,datetime_expr) --timestampdiff(unit,datetime_expr1,datetime_expr2) --

请看示例部分:

select timestamp('2008-08-08'); -- 2008-08-08 00:00:00select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00MySQL timestampadd() 函数类似于 date_add()。select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12select datediff('2008-08-08 12:00:00', '2008-08-01 00:00:00'); -- 7

提示:MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff() 只能计算两个日期(date)之间相差的天数。

五、MySQL 时区timezone转换函数

convert_tz(dt,from_tz,to_tz)select convert_tz('2008-08-08 12:00:00', '+08:00', '+00:00'); -- 2008-08-08 04:00:00时区转换也可以通过 date_add, date_sub, timestampadd 来实现。select date_add('2008-08-08 12:00:00', interval -8 hour); -- 2008-08-08 04:00:00select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00

转载于:https://blog.51cto.com/wujianwei/2175810

你可能感兴趣的文章
“亲切照料”下的领域驱动设计
查看>>
SRE工程师到底是做什么的?
查看>>
解读:Red Hat为什么收购Ansible
查看>>
Ossim下的安全合规管理
查看>>
DelphiWebMVC框架下BPL热部署实现
查看>>
C++与MySQL的冲突
查看>>
siki学习之观察者模式笔记
查看>>
单元测试
查看>>
spring.net 继承
查看>>
ES6:模块简单解释
查看>>
JavaScript indexOf() 方法
查看>>
用Bootstrap写一份简历
查看>>
ZJU PAT 1023
查看>>
WMI远程访问问题解决方法
查看>>
从零开始学习IOS,(UILabel控件)详细使用和特殊效果
查看>>
Android开发历程_15(AppWidget的使用)
查看>>
阿花宝宝 Java 笔记 之 初识java
查看>>
7、设计模式-创建型模式-建造者模式
查看>>
我国古代的勾股定理
查看>>
Linux下的C编程实战
查看>>