【MySQL】MySQL日期函数大全及详解

文章目录


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

select now();
+---------------------+
| now()               |
+---------------------+
| 2018-12-25 20:23:49 |
+---------------------+
1 row in set (0.00 sec)

除了 now() 函数能获得当前的日期时间外,MySQL 中还有下面的函数:

current_timestamp()/current_timestamp

select current_timestamp();
+---------------------+
| current_timestamp() |
+---------------------+
| 2018-12-25 20:26:47 |
+---------------------+
1 row in set (0.00 sec)
select current_timestamp;
+---------------------+
| current_timestamp   |
+---------------------+
| 2018-12-25 20:28:42 |
+---------------------+
1 row in set (0.00 sec)

localtime()/localtime

select localtime();
+---------------------+
| localtime()         |
+---------------------+
| 2018-12-25 20:29:57 |
+---------------------+
1 row in set (0.00 sec)
select localtime;
+---------------------+
| localtime           |
+---------------------+
| 2018-12-25 20:30:08 |
+---------------------+
1 row in set (0.00 sec)

localtimestamp()/localtimestamp — (v4.0.6)

select localtimestamp();
+---------------------+
| localtimestamp()    |
+---------------------+
| 2018-12-25 20:30:26 |
+---------------------+
1 row in set (0.00 sec)
select localtimestamp;
+---------------------+
| localtimestamp      |
+---------------------+
| 2018-12-25 20:30:35 |
+---------------------+
1 row in set (0.00 sec)

这些日期时间函数,都等同于 now()。鉴于 now() 函数简短易记,建议总是使用 now() 来替代上面列出的函数。

获得当前日期+时间(date + time)函数:sysdate()

sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值。看下面的例子就明白了:

select now(), sleep(3), now();
+---------------------+----------+---------------------+
| now()               | sleep(3) | now()               |
+---------------------+----------+---------------------+
| 2018-12-25 20:32:33 |        0 | 2018-12-25 20:32:33 |
+---------------------+----------+---------------------+
1 row in set (3.00 sec)
select sysdate(), sleep(3), sysdate();
+---------------------+----------+---------------------+
| sysdate()           | sleep(3) | sysdate()           |
+---------------------+----------+---------------------+
| 2018-12-25 20:33:14 |        0 | 2018-12-25 20:33:17 |
+---------------------+----------+---------------------+
1 row in set (3.00 sec)

可以看到,虽然中途 sleep 3 秒,但 now() 函数两次的时间值是相同的; sysdate() 函数两次得到的时间值相差 3 秒。MySQL Manual 中是这样描述 sysdate() 的:Return the time at which the function executes。
sysdate() 日期时间函数,一般情况下很少用到。

获得当前日期(date)函数:curdate()

select curdate();
+------------+
| curdate()  |
+------------+
| 2018-12-25 |
+------------+
1 row in set (0.00 sec)

其中,下面的两个日期函数等同于 curdate():
current_date()/current_date

select current_date();
+----------------+
| current_date() |
+----------------+
| 2018-12-25     |
+----------------+
1 row in set (0.00 sec)
select current_date;
+--------------+
| current_date |
+--------------+
| 2018-12-25   |
+--------------+
1 row in set (0.00 sec)

获得当前时间(time)函数:curtime()

select curtime();
+-----------+
| curtime() |
+-----------+
| 20:37:05  |
+-----------+
1 row in set (0.00 sec)

其中,下面的两个时间函数等同于 curtime():
current_time()/current_time

select current_time();
+----------------+
| current_time() |
+----------------+
| 20:37:55       |
+----------------+
1 row in set (0.00 sec)
select current_time;
+--------------+
| current_time |
+--------------+
| 20:38:06     |
+--------------+
1 row in set (0.00 sec)

获得当前 UTC 日期时间函数:utc_date(), utc_time(), utc_timestamp()

select utc_timestamp(), utc_date(), utc_time(),now();
+---------------------+------------+------------+---------------------+
| utc_timestamp()     | utc_date() | utc_time() | now()               |
+---------------------+------------+------------+---------------------+
| 2018-12-25 12:40:15 | 2018-12-25 | 12:40:15   | 2018-12-25 20:40:15 |
+---------------------+------------+------------+---------------------+
1 row in set (0.00 sec)

因为我国位于东八时区,所以本地时间 = UTC 时间 + 8 小时。UTC 时间在业务涉及多个国家和地区的时候,非常有用。

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

set @dt = '2018-12-25 22:15:30.123456';
select date(@dt);
+------------+
| date(@dt)  |
+------------+
| 2018-12-25 |
+------------+
1 row in set (0.00 sec)
select time(@dt);
+-----------------+
| time(@dt)       |
+-----------------+
| 22:15:30.123456 |
+-----------------+
1 row in set (0.00 sec)
select year(@dt);
+-----------+
| year(@dt) |
+-----------+
|      2018 |
+-----------+
1 row in set (0.00 sec)
select year('70-12-25'); -- 
+------------------+
| year('70-12-25') |
+------------------+
|             1970 |
+------------------+
1 row in set (0.00 sec)
select year('69-12-25');
+------------------+
| year('69-12-25') |
+------------------+
|             2069 |
+------------------+
1 row in set (0.00 sec)

要注意的是: 如果年份只有两位数,那么自动补全的机制是以默认时间1970.01.01为界限的,>= 70 的补全 19,< 70 的补全 20

select quarter(@dt); -- 第几季度
+--------------+
| quarter(@dt) |
+--------------+
|            4 |
+--------------+
1 row in set (0.00 sec)
select month(@dt);
+------------+
| month(@dt) |
+------------+
|         12 |
+------------+
1 row in set (0.00 sec)
select week(@dt); -- 一年的第几周
+-----------+
| week(@dt) |
+-----------+
|        51 |
+-----------+
1 row in set (0.00 sec)
select week(@dt, 0); -- 一年当中的第几周,当index为偶数时,默认是以为周日作为一周的第一天
+--------------+
| week(@dt, 0) |
+--------------+
|           51 |
+--------------+
1 row in set (0.00 sec)
select week(@dt, 1); -- 当index为奇数时,表示一周的第一天是周一
+--------------+
| week(@dt, 1) |
+--------------+
|           52 |
+--------------+
1 row in set (0.00 sec)
select day(@dt);  -- 一个月中的第多少天
+----------+
| day(@dt) |
+----------+
|       25 |
+----------+
1 row in set (0.00 sec)
select hour(@dt); -- 一天中的小时
+-----------+
| hour(@dt) |
+-----------+
|        22 |
+-----------+
1 row in set (0.00 sec)
select minute(@dt); -- 小时中的分钟
+-------------+
| minute(@dt) |
+-------------+
|          15 |
+-------------+
1 row in set (0.01 sec)
select second(@dt); -- 时间中的秒
+-------------+
| second(@dt) |
+-------------+
|          30 |
+-------------+
1 row in set (0.00 sec)

Extract() 函数,可以上面实现类似的功能:

set @dt = '2018-12-25 22:15:30.123456';
select extract(year from @dt);
+------------------------+
| extract(year from @dt) |
+------------------------+
|                   2018 |
+------------------------+
1 row in set (0.00 sec)
select extract(quarter from @dt);
+---------------------------+
| extract(quarter from @dt) |
+---------------------------+
|                         4 |
+---------------------------+
1 row in set (0.00 sec)
select extract(month from @dt);
+-------------------------+
| extract(month from @dt) |
+-------------------------+
|                      12 |
+-------------------------+
1 row in set (0.00 sec)
select extract(week from @dt); -- 一年中的第多少周
+------------------------+
| extract(week from @dt) |
+------------------------+
|                     51 |
+------------------------+
1 row in set (0.00 sec)
select extract(day from @dt); -- 当月的第多少天
+-----------------------+
| extract(day from @dt) |
+-----------------------+
|                    25 |
+-----------------------+
1 row in set (0.00 sec)
select extract(hour from @dt); -- 时间中的小时
+------------------------+
| extract(hour from @dt) |
+------------------------+
|                     22 |
+------------------------+
1 row in set (0.00 sec)
select extract(minute from @dt); -- 时间中的分钟
+--------------------------+
| extract(minute from @dt) |
+--------------------------+
|                       15 |
+--------------------------+
1 row in set (0.00 sec)
select extract(second from @dt);  -- 时间中的秒
+--------------------------+
| extract(second from @dt) |
+--------------------------+
|                       30 |
+--------------------------+
1 row in set (0.01 sec)
select extract(microsecond from @dt); -- 时间中的微秒
+-------------------------------+
| extract(microsecond from @dt) |
+-------------------------------+
|                        123456 |
+-------------------------------+
1 row in set (0.00 sec)
select extract(year_month from @dt); -- 年月
+------------------------------+
| extract(year_month from @dt) |
+------------------------------+
|                       201812 |
+------------------------------+
1 row in set (0.00 sec)
select extract(day_hour from @dt); -- 天和小时
+----------------------------+
| extract(day_hour from @dt) |
+----------------------------+
|                       2522 |
+----------------------------+
1 row in set (0.00 sec)
select extract(day_minute from @dt); -- 天到分钟
+------------------------------+
| extract(day_minute from @dt) |
+------------------------------+
|                       252215 |
+------------------------------+
1 row in set (0.00 sec)
select extract(day_second from @dt); -- 天到秒
+------------------------------+
| extract(day_second from @dt) |
+------------------------------+
|                     25221530 |
+------------------------------+
1 row in set (0.00 sec)
select extract(day_microsecond from @dt); -- 天到微秒
+-----------------------------------+
| extract(day_microsecond from @dt) |
+-----------------------------------+
|                    25221530123456 |
+-----------------------------------+
1 row in set (0.00 sec)
select extract(hour_minute from @dt); -- 小时和分钟
+-------------------------------+
| extract(hour_minute from @dt) |
+-------------------------------+
|                          2215 |
+-------------------------------+
1 row in set (0.00 sec)
select extract(hour_second from @dt); -- 小时到秒
+-------------------------------+
| extract(hour_second from @dt) |
+-------------------------------+
|                        221530 |
+-------------------------------+
1 row in set (0.00 sec)
select extract(hour_microsecond from @dt); -- 小时到微秒
+------------------------------------+
| extract(hour_microsecond from @dt) |
+------------------------------------+
|                       221530123456 |
+------------------------------------+
1 row in set (0.00 sec)
select extract(minute_second from @dt); -- 分钟到秒
+---------------------------------+
| extract(minute_second from @dt) |
+---------------------------------+
|                            1530 |
+---------------------------------+
1 row in set (0.00 sec)
select extract(minute_microsecond from @dt); -- 分钟到微秒
+--------------------------------------+
| extract(minute_microsecond from @dt) |
+--------------------------------------+
|                           1530123456 |
+--------------------------------------+
1 row in set (0.00 sec)
select extract(second_microsecond from @dt); -- 秒到微秒
+--------------------------------------+
| extract(second_microsecond from @dt) |
+--------------------------------------+
|                             30123456 |
+--------------------------------------+
1 row in set (0.00 sec)

MySQL Extract() 函数除了没有date(),time() 的功能外,其他功能一应具全。并且还具有选取‘day_microsecond’ 等功能。注意这里不是只选取 day 和 microsecond,而是从日期的 day 部分一直选取到 microsecond 部分。

MySQL时间对应的单位

单位所能取的值期望的格式
MICROSECOND毫秒
SECOND
MINUTE
HOUR小时
DAY
WEEK
MONTH
QUARTER季度
YEAR
SECOND_MICROSECOND‘秒.毫秒’
MINUTE_MICROSECOND‘分.毫秒’
MINUTE_SECOND‘分:秒’
HOUR_MICROSECOND‘小时.毫秒’
HOUR_SECOND‘小时:分:秒’
HOUR_MINUTE‘小时:分’
DAY_MICROSECOND‘日.毫秒’
DAY_SECOND‘日 小时:分:秒’
DAY_MINUTE‘日 小时:分’
DAY_HOUR‘日 小时’
YEAR_MONTH‘年-月’

dayof… 函数:dayofweek(), dayofmonth(), dayofyear() 分别返回日期参数,在一周、一月、一年中的位置

set @dt = '2018-12-25 22:15:30.123456';
select dayofweek(@dt); -- 一周中的第几天,1= Sunday, 2 = Monday, …, 7 = Saturday
+----------------+
| dayofweek(@dt) |
+----------------+
|              3 |
+----------------+
1 row in set (0.00 sec)
select weekday(@dt); -- 表示返回date是在一周中的序号,表示返回date是在一周中的序号,西方日历中通常一周的开始是星期天,并且以0开始计数,所以,记住:0=星期一,1=星期二, ... 5=星期六)
+--------------+
| weekday(@dt) |
+--------------+
|            1 |
+--------------+
1 row in set (0.00 sec)
select dayofmonth(@dt); -- 一月中的第几天,1号就返回1,... ,31号就返回31
+-----------------+
| dayofmonth(@dt) |
+-----------------+
|              25 |
+-----------------+
1 row in set (0.00 sec)
select dayofyear(@dt); -- 一年中的第几天
+----------------+
| dayofyear(@dt) |
+----------------+
|            359 |
+----------------+
1 row in set (0.00 sec)

注意: dayofweek()一周中的第几天是这样算的:1= Sunday, 2 = Monday, …, 7 = Saturday
weekday():表示返回date是在一周中的序号,西方日历中通常一周的开始是星期天,并且以0开始计数,所以,记住:0=星期一,1=星期二, … 5=星期六)

返回星期和月份名称函数:dayname(), monthname()

set @dt = '2018-12-25 22:15:30.123456';
select dayname(@dt); -- 表示返回date是周几的英文全称名字
+--------------+
| dayname(@dt) |
+--------------+
| Tuesday      |
+--------------+
1 row in set (0.00 sec)
select monthname(@dt);
+----------------+
| monthname(@dt) |
+----------------+
| December       |
+----------------+
1 row in set (0.00 sec)

last_day() 函数:返回月份中的最后一天

hive 中也可以使用

set @dt = '2018-12-25 22:15:30.123456';
select last_day(@dt);
+---------------+
| last_day(@dt) |
+---------------+
| 2018-12-31    |
+---------------+
1 row in set (0.00 sec)

MySQL last_day() 函数非常有用,比如我想得到当前月份中有多少天,可以这样来计算:

select now(), day(last_day(now())) as days;
+---------------------+------+
| now()               | days |
+---------------------+------+
| 2018-12-25 23:43:07 |   31 |
+---------------------+------+
1 row in set (0.00 sec)

日期增加一个时间间隔:date_add()

set @dt = now();
select date_add(@dt, interval 1 day);
+-------------------------------+
| date_add(@dt, interval 1 day) |
+-------------------------------+
| 2018-12-27 11:29:44           |
+-------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval 1 hour);
+--------------------------------+
| date_add(@dt, interval 1 hour) |
+--------------------------------+
| 2018-12-26 12:29:44            |
+--------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval 1 minute);
+----------------------------------+
| date_add(@dt, interval 1 minute) |
+----------------------------------+
| 2018-12-26 11:30:44              |
+----------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval 1 second);
+----------------------------------+
| date_add(@dt, interval 1 second) |
+----------------------------------+
| 2018-12-26 11:29:45              |
+----------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval 1 microsecond);
+---------------------------------------+
| date_add(@dt, interval 1 microsecond) |
+---------------------------------------+
| 2018-12-26 11:29:44.000001            |
+---------------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval 1 week);
+--------------------------------+
| date_add(@dt, interval 1 week) |
+--------------------------------+
| 2019-01-02 11:29:44            |
+--------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval 1 month);
+---------------------------------+
| date_add(@dt, interval 1 month) |
+---------------------------------+
| 2019-01-26 11:29:44             |
+---------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval 1 quarter);
+-----------------------------------+
| date_add(@dt, interval 1 quarter) |
+-----------------------------------+
| 2019-03-26 11:29:44               |
+-----------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval 1 year);
+--------------------------------+
| date_add(@dt, interval 1 year) |
+--------------------------------+
| 2019-12-26 11:29:44            |
+--------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval -1 day); -- 可以传负数,表示减
+--------------------------------+
| date_add(@dt, interval -1 day) |
+--------------------------------+
| 2018-12-25 11:29:44            |
+--------------------------------+
1 row in set (0.00 sec)

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

select addtime(@dt, '01:30:30');
+--------------------------+
| addtime(@dt, '01:30:30') |
+--------------------------+
| 2018-12-26 13:00:14      |
+--------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval '01:15:30' hour_second);
+------------------------------------------------+
| date_add(@dt, interval '01:15:30' hour_second) |
+------------------------------------------------+
| 2018-12-26 12:45:14                            |
+------------------------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval '1 01:15:30' day_second);
+-------------------------------------------------+
| date_add(@dt, interval '1 01:15:30' day_second) |
+-------------------------------------------------+
| 2018-12-27 12:45:14                             |
+-------------------------------------------------+
1 row in set (0.00 sec)

select date_add(@dt, interval '1 01:15:30' day_microsecond);
+------------------------------------------------------+
| date_add(@dt, interval '1 01:15:30' day_microsecond) |
+------------------------------------------------------+
| 2018-12-26 12:30:59.300000                           |
+------------------------------------------------------+
1 row in set (0.00 sec)

date_add() 函数,分别为 @dt 增加了“1小时 15分 30秒” 和 “1天 1小时 15分 30秒”。建议:总是使用 date_add() 日期时间函数来替代 adddate(), addtime()。

日期减去一个时间间隔:date_sub()

select date_sub(@dt, interval '1 01:15:30' day_microsecond);
+------------------------------------------------------+
| date_sub(@dt, interval '1 01:15:30' day_microsecond) |
+------------------------------------------------------+
| 2018-12-26 10:28:28.700000                           |
+------------------------------------------------------+
1 row in set (0.00 sec)

MySQL date_sub() 日期时间函数 和 date_add() 用法一致,不再赘述。另外,MySQL 中还有两个函数 subdate(), subtime(),建议,用 date_sub() 来替代。

另类日期函数:period_add(P,N), period_diff(P1,P2)

period_add(P,N)函数参数“P” 的格式为“YYYYMM” 或者 “YYMM”,第二个参数“N” 表示增加或减去 N month(月)

select period_add(201812,2), period_add(20181226,-2);
+----------------------+-------------------------+
| period_add(201812,2) | period_add(20181226,-2) |
+----------------------+-------------------------+
|               201902 |                20181312 |
+----------------------+-------------------------+
1 row in set (0.00 sec)

period_diff(P1,P2):日期 P1-P2,返回 N 个月。

select period_diff(201812, 201808);
+-----------------------------+
| period_diff(201812, 201808) |
+-----------------------------+
|                           4 |
+-----------------------------+
1 row in set (0.00 sec)

在 MySQL 中,这两个日期函数,一般情况下很少用到。

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

datediff(date1,date2):两个日期相减 date1 – date2,返回天数

select datediff('2018-12-26', '2018-08-01');
+--------------------------------------+
| datediff('2018-12-26', '2018-08-01') |
+--------------------------------------+
|                                  147 |
+--------------------------------------+
1 row in set (0.00 sec)
select datediff('2018-08-26', '2018-12-26');
+--------------------------------------+
| datediff('2018-08-26', '2018-12-26') |
+--------------------------------------+
|                                 -122 |
+--------------------------------------+
1 row in set (0.00 sec)

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

select timediff('2018-12-26 08:08:08', '2018-08-08 00:00:00'); 
+--------------------------------------------------------+
| timediff('2018-12-26 08:08:08', '2018-08-08 00:00:00') |
+--------------------------------------------------------+
| 838:59:59                                              |
+--------------------------------------------------------+
1 row in set, 1 warning (0.00 sec)

select timediff('2018-08-08 08:08:08', '2018-12-08 00:00:00'); 
+--------------------------------------------------------+
| timediff('2018-08-08 08:08:08', '2018-12-08 00:00:00') |
+--------------------------------------------------------+
| -838:59:59                                             |
+--------------------------------------------------------+
1 row in set, 1 warning (0.01 sec)

select timediff('08:08:08', '00:00:00'); 
+----------------------------------+
| timediff('08:08:08', '00:00:00') |
+----------------------------------+
| 08:08:08                         |
+----------------------------------+
1 row in set (0.00 sec)

日期转换函数、时间转换函数

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

select time_to_sec('10:00:00');
+-------------------------+
| time_to_sec('10:00:00') |
+-------------------------+
|                   36000 |
+-------------------------+
1 row in set (0.00 sec)

select sec_to_time(36000);
+--------------------+
| sec_to_time(36000) |
+--------------------+
| 10:00:00           |
+--------------------+
1 row in set (0.00 sec)

(日期、天数)转换函数:to_days(date), from_days(days)

to_days(date)函数’0000-01-01’返回的1,小于’0000-01-01’都返回NULL
from_days(days)函数小于366返回的都是’0000-00-00’,366返回的是’0001-01-01’

select to_days('0000-01-01');
+-----------------------+
| to_days('0000-01-01') |
+-----------------------+
|                     1 |
+-----------------------+
1 row in set (0.00 sec)

select to_days('2018-12-26');
+-----------------------+
| to_days('2018-12-26') |
+-----------------------+
|                737419 |
+-----------------------+
1 row in set (0.00 sec)

select from_days(365);
+----------------+
| from_days(365) |
+----------------+
| 0000-00-00     |
+----------------+
1 row in set (0.00 sec)

select from_days(366);
+----------------+
| from_days(366) |
+----------------+
| 0001-01-01     |
+----------------+
1 row in set (0.00 sec)

select from_days(737419);
+-------------------+
| from_days(737419) |
+-------------------+
| 2018-12-26        |
+-------------------+
1 row in set (0.00 sec)

str_to_date(字符串转换为日期)函数:str_to_date(str, format)

select str_to_date('12/26/2018', '%m/%d/%Y');
+---------------------------------------+
| str_to_date('12/26/2018', '%m/%d/%Y') |
+---------------------------------------+
| 2018-12-26                            |
+---------------------------------------+
1 row in set (0.00 sec)

select str_to_date('12/26/18' , '%m/%d/%y'); 
+--------------------------------------+
| str_to_date('12/26/18' , '%m/%d/%y') |
+--------------------------------------+
| 2018-12-26                           |
+--------------------------------------+
1 row in set (0.00 sec)

select str_to_date('12.26.2018', '%m.%d.%Y'); 
+---------------------------------------+
| str_to_date('12.26.2018', '%m.%d.%Y') |
+---------------------------------------+
| 2018-12-26                            |
+---------------------------------------+
1 row in set (0.00 sec)

select str_to_date('12:12:12', '%h:%i:%s');
+-------------------------------------+
| str_to_date('12:12:12', '%h:%i:%s') |
+-------------------------------------+
| 00:12:12                            |
+-------------------------------------+
1 row in set (0.00 sec)
 
select str_to_date('12.26.2018 08:00:08', '%m.%d.%Y %h:%i:%s');
+---------------------------------------------------------+
| str_to_date('12.26.2018 08:00:08', '%m.%d.%Y %h:%i:%s') |
+---------------------------------------------------------+
| 2018-12-26 08:00:08                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

可以看到,str_to_date(str,format) 转换函数,可以把一些杂乱无章的字符串转换为日期格式。另外,它也可以转换为时间。“format” 可以参看MySQL手册。

MySQL中日期格式

格式标识符说明
%a一星期中每天名称的缩写(Sun…Sat)
%b月份的缩写(Jan…Dec)
%c月份的数字表现形式(0…12)
%D带有英语后缀的一个月中的每一天的名称(0th、1st、2nd、3rd)
%d用数字形式表现的每月中的每一天(00…31)
%e用数字形式表现的每月中的每一天(0…31)
%f毫秒(000000…999999)
%H24时制显示的小时(00…23)
%h12时制显示的小时(01…12)
%I12时制显示的小时(01…12)
%i以数字形式表现的分钟数(00…59)
%j一年中的每一天(001…366)
%k24时制小时的另一种表现格式(0…23)
%l12时制小时的另一种表现格式(1…12)
%M用完整英文名称表示的月份(January…December)
%m用数字表现的月份(00…12)
%p上午(AM)或下午(PM)
%r12时制的时间值(hh:mm:ss,后跟 AM 或 PM)
%S秒(00…59)
%s秒(00…59)
%T24时制的小时(hh:mm:ss)
%U星期(00…53),其中星期天是每星期的开始日
%u星期(00…53),其中星期一是每星期的开始日
%V星期(01…53),其中星期天是每星期的开始日,和 %X 一起使用
%v星期(01…53),其中星期一是每星期的开始日,和 %x 一起使用
%W一星期中各日名称(Sunday…Saturday)
%w一星期中各日名称(0代表星期日,6代表星期六,以此类推)
%X某星期所处年份。其中,星期天是每星期的开始日,采用4位数字形式表现,和 %V一起使用
%x某星期所处年份。其中,星期一是每星期的开始日,采用4位数字形式表现,和 %V 一起使用
%Y4位数字表示的年份
%y2位数字表示的年份
%%符号%的字面值
%x(x为斜体)字符x的字面值,x指以上未列出的任何字符

Date/Time to Str(日期/时间转换为字符串)函数:date_format(date,format), time_format(time,format)

select date_format('2018-12-26 22:23:00', '%W %M %Y');
+------------------------------------------------+
| date_format('2018-12-26 22:23:00', '%W %M %Y') |
+------------------------------------------------+
| Wednesday December 2018                        |
+------------------------------------------------+
1 row in set (0.00 sec)

select date_format('2018-12-26 22:23:00', '%Y %m %d %H %m %s');
+---------------------------------------------------------+
| date_format('2018-12-26 22:23:00', '%Y %m %d %H %m %s') |
+---------------------------------------------------------+
| 2018 12 26 22 12 00                                     |
+---------------------------------------------------------+
1 row in set (0.00 sec)

select date_format('2018-12-26 22:23:01', '%Y%m%d%H%i%s');
+----------------------------------------------------+
| date_format('2018-12-26 22:23:01', '%Y%m%d%H%i%s') |
+----------------------------------------------------+
| 20181226222301                                     |
+----------------------------------------------------+
1 row in set (0.00 sec)

select time_format('22:23:01', '%H.%i.%s');
+-------------------------------------+
| time_format('22:23:01', '%H.%i.%s') |
+-------------------------------------+
| 22.23.01                            |
+-------------------------------------+
1 row in set (0.00 sec)

select time_format('22:23:01', '%H:%i:%s');
+-------------------------------------+
| time_format('22:23:01', '%H:%i:%s') |
+-------------------------------------+
| 22:23:01                            |
+-------------------------------------+
1 row in set (0.00 sec)

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

获得国家地区时间格式函数:get_format()

MySQL get_format() 语法:

get_format(date|time|datetime, ‘eur’|’usa’|’jis’|’iso’|’internal’)

MySQL get_format() 用法的全部示例:

select get_format(date,'usa');
+------------------------+
| get_format(date,'usa') |
+------------------------+
| %m.%d.%Y               |
+------------------------+
1 row in set (0.00 sec)

select get_format(date,'jis');
+------------------------+
| get_format(date,'jis') |
+------------------------+
| %Y-%m-%d               |
+------------------------+
1 row in set (0.00 sec)

select get_format(date,'iso');
+------------------------+
| get_format(date,'iso') |
+------------------------+
| %Y-%m-%d               |
+------------------------+
1 row in set (0.00 sec)

select get_format(date,'eur');
+------------------------+
| get_format(date,'eur') |
+------------------------+
| %d.%m.%Y               |
+------------------------+
1 row in set (0.00 sec)

select get_format(date,'internal');
+-----------------------------+
| get_format(date,'internal') |
+-----------------------------+
| %Y%m%d                      |
+-----------------------------+
1 row in set (0.00 sec)

select get_format(datetime,'usa');
+----------------------------+
| get_format(datetime,'usa') |
+----------------------------+
| %Y-%m-%d %H.%i.%s          |
+----------------------------+
1 row in set (0.00 sec)

select get_format(datetime,'jis');
+----------------------------+
| get_format(datetime,'jis') |
+----------------------------+
| %Y-%m-%d %H:%i:%s          |
+----------------------------+
1 row in set (0.00 sec)

select get_format(datetime,'iso');
+----------------------------+
| get_format(datetime,'iso') |
+----------------------------+
| %Y-%m-%d %H:%i:%s          |
+----------------------------+
1 row in set (0.00 sec)

select get_format(datetime,'eur');
+----------------------------+
| get_format(datetime,'eur') |
+----------------------------+
| %Y-%m-%d %H.%i.%s          |
+----------------------------+
1 row in set (0.00 sec)

select get_format(datetime,'internal');
+---------------------------------+
| get_format(datetime,'internal') |
+---------------------------------+
| %Y%m%d%H%i%s                    |
+---------------------------------+
1 row in set (0.00 sec)

select get_format(time,'usa');
+------------------------+
| get_format(time,'usa') |
+------------------------+
| %h:%i:%s %p            |
+------------------------+
1 row in set (0.00 sec)

select get_format(time,'jis');
+------------------------+
| get_format(time,'jis') |
+------------------------+
| %H:%i:%s               |
+------------------------+
1 row in set (0.00 sec)

select get_format(time,'iso');
+------------------------+
| get_format(time,'iso') |
+------------------------+
| %H:%i:%s               |
+------------------------+
1 row in set (0.00 sec)

select get_format(time,'eur');
+------------------------+
| get_format(time,'eur') |
+------------------------+
| %H.%i.%s               |
+------------------------+
1 row in set (0.00 sec)

select get_format(time,'internal');
+-----------------------------+
| get_format(time,'internal') |
+-----------------------------+
| %H%i%s                      |
+-----------------------------+
1 row in set (0.00 sec)

其实,MySQL中 get_format() 函数在实际中用到机会的比较少。

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

select makedate(2018,31);
+-------------------+
| makedate(2018,31) |
+-------------------+
| 2018-01-31        |
+-------------------+
1 row in set (0.00 sec)

select makedate(2018,66);
+-------------------+
| makedate(2018,66) |
+-------------------+
| 2018-03-07        |
+-------------------+
1 row in set (0.00 sec)

select maketime(23,30,30);
+--------------------+
| maketime(23,30,30) |
+--------------------+
| 23:30:30           |
+--------------------+
1 row in set (0.00 sec)

时间戳(Timestamp)函数

获得当前时间戳函数:current_timestamp, current_timestamp()

select current_timestamp, current_timestamp();
+---------------------+---------------------+
| current_timestamp   | current_timestamp() |
+---------------------+---------------------+
| 2018-12-26 23:31:55 | 2018-12-26 23:31:55 |
+---------------------+---------------------+
1 row in set (0.00 sec)

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

unix_timestamp()

select unix_timestamp();
+------------------+
| unix_timestamp() |
+------------------+
|       1545838413 |
+------------------+
1 row in set (0.00 sec)

unix_timestamp(date)

select unix_timestamp('2018-12-26');
+------------------------------+
| unix_timestamp('2018-12-26') |
+------------------------------+
|                   1545753600 |
+------------------------------+
1 row in set (0.00 sec)

select unix_timestamp('2018-12-26 23:40:50');
+---------------------------------------+
| unix_timestamp('2018-12-26 23:40:50') |
+---------------------------------------+
|                            1545838850 |
+---------------------------------------+
1 row in set (0.00 sec)

from_unixtime(unix_timestamp)

select from_unixtime(1545753600);
+---------------------------+
| from_unixtime(1545753600) |
+---------------------------+
| 2018-12-26 00:00:00       |
+---------------------------+
1 row in set (0.00 sec)

from_unixtime(unix_timestamp,format)

select from_unixtime(1545838850,'%Y-%m-%d %H:%i:%s');
+-----------------------------------------------+
| from_unixtime(1545838850,'%Y-%m-%d %H:%i:%s') |
+-----------------------------------------------+
| 2018-12-26 23:40:50                           |
+-----------------------------------------------+
1 row in set (0.00 sec)

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

timestamp(date)

select timestamp('2018-12-26 23:40:50');
+----------------------------------+
| timestamp('2018-12-26 23:40:50') |
+----------------------------------+
| 2018-12-26 23:40:50              |
+----------------------------------+
1 row in set (0.00 sec)

select timestamp('2018-12-26');
+-------------------------+
| timestamp('2018-12-26') |
+-------------------------+
| 2018-12-26 00:00:00     |
+-------------------------+
1 row in set (0.00 sec)

timestamp(dt,time)

select timestamp('2018-12-26','10:20:30');
+------------------------------------+
| timestamp('2018-12-26','10:20:30') |
+------------------------------------+
| 2018-12-26 10:20:30                |
+------------------------------------+
1 row in set (0.00 sec)

select timestamp('2018-12-26 10:10:10','10:20:30');
+---------------------------------------------+
| timestamp('2018-12-26 10:10:10','10:20:30') |
+---------------------------------------------+
| 2018-12-26 20:30:40                         |
+---------------------------------------------+
1 row in set (0.00 sec)

timestampadd(unit,interval,datetime_expr)

select date_add('2018-08-08 08:08:08', interval 1 day);
+-------------------------------------------------+
| date_add('2018-08-08 08:08:08', interval 1 day) |
+-------------------------------------------------+
| 2018-08-09 08:08:08                             |
+-------------------------------------------------+
1 row in set (0.02 sec)

select timestampadd(year, 1, '2018-12-26');
+-------------------------------------+
| timestampadd(year, 1, '2018-12-26') |
+-------------------------------------+
| 2019-12-26                          |
+-------------------------------------+
1 row in set (0.00 sec)

select timestampadd(month, 1, '2018-12-26');
+--------------------------------------+
| timestampadd(month, 1, '2018-12-26') |
+--------------------------------------+
| 2019-01-26                           |
+--------------------------------------+
1 row in set (0.00 sec)

select timestampadd(day, 1, '2018-12-26');
+------------------------------------+
| timestampadd(day, 1, '2018-12-26') |
+------------------------------------+
| 2018-12-27                         |
+------------------------------------+
1 row in set (0.00 sec)

select timestampadd(hour, 1, '2018-12-26');
+-------------------------------------+
| timestampadd(hour, 1, '2018-12-26') |
+-------------------------------------+
| 2018-12-26 01:00:00                 |
+-------------------------------------+
1 row in set (0.00 sec)

select timestampadd(hour, 1, '2018-12-26 23:00:00');
+----------------------------------------------+
| timestampadd(hour, 1, '2018-12-26 23:00:00') |
+----------------------------------------------+
| 2018-12-27 00:00:00                          |
+----------------------------------------------+
1 row in set (0.00 sec)

select timestampadd(second, '1', '2018-12-26 23:00:00');
+------------------------------------------------+
| timestampadd(second, 1, '2018-12-26 23:00:00') |
+------------------------------------------------+
| 2018-12-26 23:00:01                            |
+------------------------------------------------+
1 row in set (0.00 sec)

select timestampadd(second, '1', '2018-12-26 23:00:00');
+--------------------------------------------------+
| timestampadd(second, '1', '2018-12-26 23:00:00') |
+--------------------------------------------------+
| 2018-12-26 23:00:01                              |
+--------------------------------------------------+
1 row in set (0.00 sec)

timestampdiff(unit,datetime_expr1,datetime_expr2)

select timestampdiff(year,'2018-12-26','2001-01-01');
+-----------------------------------------------+
| timestampdiff(year,'2018-12-26','2001-01-01') |
+-----------------------------------------------+
|                                           -17 |
+-----------------------------------------------+
1 row in set (0.00 sec)

select timestampdiff(month ,'2008-12-26','2019-01-01'); 
+-------------------------------------------------+
| timestampdiff(month ,'2008-12-26','2019-01-01') |
+-------------------------------------------------+
|                                             120 |
+-------------------------------------------------+
1 row in set (0.00 sec)

select timestampdiff(day ,'2008-12-26','2019-01-01'); 
+-----------------------------------------------+
| timestampdiff(day ,'2008-12-26','2019-01-01') |
+-----------------------------------------------+
|                                          3658 |
+-----------------------------------------------+
1 row in set (0.00 sec)

select timestampdiff(hour,'2018-12-26','2019-01-01'); 
+-----------------------------------------------+
| timestampdiff(hour,'2018-12-26','2019-01-01') |
+-----------------------------------------------+
|                                           144 |
+-----------------------------------------------+
1 row in set (0.00 sec)

select timestampdiff(minute,'2018-12-30','2019-01-01'); 
+-------------------------------------------------+
| timestampdiff(minute,'2018-12-30','2019-01-01') |
+-------------------------------------------------+
|                                            2880 |
+-------------------------------------------------+
1 row in set (0.00 sec)

select timestampdiff(second,'2018-12-31 23:59:50','2019-01-01 00:00:59'); 
+-------------------------------------------------------------------+
| timestampdiff(second,'2018-12-31 23:59:50','2019-01-01 00:00:59') |
+-------------------------------------------------------------------+
|                                                                69 |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)

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

时区(timezone)转换函数convert_tz(dt,from_tz,to_tz)

select convert_tz('2018-12-26 23:00:00',  '+08:00', '+00:00'); 
+--------------------------------------------------------+
| convert_tz('2018-12-26 23:00:00',  '+08:00', '+00:00') |
+--------------------------------------------------------+
| 2018-12-26 15:00:00                                    |
+--------------------------------------------------------+
1 row in set (0.00 sec)

时区转换也可以通过 date_add, date_sub, timestampadd 来实现。

select date_add('2018-12-26 23:00:00', interval -8 hour);
+---------------------------------------------------+
| date_add('2018-12-26 23:00:00', interval -8 hour) |
+---------------------------------------------------+
| 2018-12-26 15:00:00                               |
+---------------------------------------------------+
1 row in set (0.00 sec)

select date_sub('2018-12-26 23:00:00', interval 8 hour); 
+--------------------------------------------------+
| date_sub('2018-12-26 23:00:00', interval 8 hour) |
+--------------------------------------------------+
| 2018-12-26 15:00:00                              |
+--------------------------------------------------+
1 row in set (0.00 sec)

select timestampadd(hour, -8, '2018-12-26 23:00:00'); 
+-----------------------------------------------+
| timestampadd(hour, -8, '2018-12-26 23:00:00') |
+-----------------------------------------------+
| 2018-12-26 15:00:00                           |
+-----------------------------------------------+
1 row in set (0.00 sec)
  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值