赞
踩
目录
1.3 获取当前日期+时间(date + time)函数:now(): 获取执行时间的值
1.4 获取当前日期+时间(date + time)函数:sysdate()
1.5 获取当前时间戳函数:current_timestamp, current_timestamp()
2.1 日期/时间转换为字符串函数:date_format(date,format), time_format(time,format)
2.2 字符串转换为日期 函数:str_to_date(str, format)
2.3 日期/天数转换 函数:to_days(date), from_days(days)
2.4 时间/秒转换 函数:time_to_sec(time), sec_to_time(seconds)
2.5 拼凑日期/时间 函数:makdedate(year,dayofyear), maketime(hour,minute,second)
3.3 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)
4.MySQL 时区(timezone)转换函数:convert_tz(dt,from_tz,to_tz)
- select str_to_date('05/08/2023', '%m/%d/%Y'); -- 2023-05-08
- select str_to_date('05/08/2023' , '%m/%d/%y'); -- 2023-05-08
- select str_to_date('05.08.2023', '%m.%d.%Y'); -- 2023-05-08
- select str_to_date('12:32:24', '%h:%i:%s'); -- 00:32:24
- select str_to_date('05.08.2023 12:32:24', '%m.%d.%Y %h:%i:%s'); -- 2023-05-08 12:32:24
- select to_days('0000-01-01'); -- 1(日期转换为天数)
- select to_days(curdate()); -- 739013(日期转换为天数)
- select from_days(0); -- 0000-00-00(天数转换为日期)
- select from_days(739013); -- 2023-05-08(天数转换为日期)
- select time_to_sec('01:00:05'); -- 3605(时间转换为秒数)
- select sec_to_time(3605); -- 01:00:05(秒数转换为时间)
- select makedate(2020,31); -- 2020-01-3(拼凑日期)
- select makedate(2020,32); -- 2020-02-01(拼凑日期)
- select maketime(12,15,30); -- 12:15:30(拼凑时间)
- select unix_timestamp(); -- 1683544672
- select from_unixtime(1683544672); -- 2023-05-08 19:17:52
-
- select unix_timestamp(curdate()); -- 1683475200
- select from_unixtime(1683475200); -- 2023-05-08 00:00:00
-
- select unix_timestamp(current_timestamp()); -- 1683544792
- select from_unixtime(1683544792); -- 2023-05-08 19:19:52
-
- select from_unixtime(1683544672, '%Y %D %M %h:%i:%s %x'); -- 2023 8th May 07:17:52 2023
- set @dt = now();
-
- select @dt;
- 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
- mysql> set @dt = now();
-
- mysql> select date_sub(@dt, interval '1 1:1:1' day_second);
- +----------------------------------------------------------------+
- | date_sub('2020-07-24 12:02:05', interval '1 1:1:1' day_second) |
- +----------------------------------------------------------------+
- | 2023-05-07 18:32:28 |
- +----------------------------------------------------------------+
- mysql> select datediff(curdate(), '2023-05-01'); -- 7
- mysql> select datediff('2023-05-01', '2023-05-08'); -- -7
- -- 转换
- select timestamp('2023-05-08'); -- 2023-05-08 00:00:00
- select timestamp('2023-05-08 19:52:51', '01:01:01'); -- 2023-05-08 20:53:52
- select timestamp(current_timestamp(), '10 01:01:01'); -- 2023-05-18 20:54:46
-
- -- 增
- select timestampadd(day, 1, current_timestamp()); -- 2023-05-09 19:54:26
- select date_add(current_timestamp(), interval 1 day); -- 2023-05-09 19:55:06
-
- -- 减
- select timestampdiff(year,current_timestamp(),'2018-01-01'); -- -5
- select timestampdiff(day ,'2023-05-08','2018-01-01'); -- -1953
- select timestampdiff(hour,'2023-05-08 12:00:00','2023-05-08 00:00:00'); -- -12
- select datediff('2023-05-08 12:00:00', '2023-05-01 00:00:00'); -- 7
-
- # MySQL timestampdiff() 函数就比 datediff() 功能强多了,datediff() 只能计算两个日期(date)之间相差的天数。
- select convert_tz('2023-05-08 12:00:00', '+08:00', '+00:00'); -- 2023-05-08 04:00:00
-
- # 时区转换也可以通过 date_add, date_sub, timestampadd 来实现。
- select date_add('2023-05-08 12:00:00', interval -8 hour); -- 2023-05-08 04:00:00
- select date_sub('2023-05-08 12:00:00', interval 8 hour); -- 2023-05-08 04:00:00
- select timestampadd(hour, -8, '2023-05-08 12:00:00'); -- 2023-05-08 04:00:00
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。