日期函数、时间函数总结
一、获取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(); -- 1218290027
select unix_timestamp('2008-08-08'); -- 1218124800
select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800
select 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 day
select date_add(@dt, interval 1 hour); -- add 1 hour
select 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-10
select time(@dt); -- 07:15:30.123456
select year(@dt); -- 2008
select quarter(@dt); -- 3
select month(@dt); -- 9
select week(@dt); -- 36
select day(@dt); -- 10
select hour(@dt); -- 7
select minute(@dt); -- 15
select second(@dt); -- 30
select 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 timestamp
timestamp(dt,time) -- dt + time
timestampadd(unit,interval,datetime_expr) --
timestampdiff(unit,datetime_expr1,datetime_expr2) --
请看示例部分:
select timestamp('2008-08-08'); -- 2008-08-08 00:00:00
select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01
select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01
select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00
select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00
MySQL timestampadd() 函数类似于 date_add()。
select timestampdiff(year,'2002-05-01','2001-01-01'); -- -1
select timestampdiff(day ,'2002-05-01','2001-01-01'); -- -485
select timestampdiff(hour,'2008-08-08 12:00:00','2008-08-08 00:00:00'); -- -12
select 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:00
select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00
select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00
转载于:https://blog.51cto.com/wujianwei/2175810