欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

MySQL函数-日期和时间函数

程序员文章站 2022-05-30 18:25:15
...

时间函数对应mysql版本5.7,低版本未测

1、获取当前日期的函数,返回格式"YYYY-MM-DD"

mysql> select curdate(),current_date();
+------------+----------------+
| curdate()  | current_date() |
+------------+----------------+
| 2019-10-30 | 2019-10-30     |
+------------+----------------+
1 row in set (0.05 sec)

2、获取当前日期的函数,返回格式"YYYY-MM-DD HH:MM:SS"

mysql> select now(),sysdate(),current_timestamp(),localtime();
+---------------------+---------------------+---------------------+---------------------+
| now()               | sysdate()           | current_timestamp() | localtime()         |
+---------------------+---------------------+---------------------+---------------------+
| 2019-10-30 14:03:46 | 2019-10-30 14:03:46 | 2019-10-30 14:03:46 | 2019-10-30 14:03:46 |
+---------------------+---------------------+---------------------+---------------------+
1 row in set (0.11 sec)

3、UNIX时间戳函数

mysql> select unix_timestamp(),unix_timestamp(now()),now();
+------------------+-----------------------+---------------------+
| unix_timestamp() | unix_timestamp(now()) | now()               |
+------------------+-----------------------+---------------------+
|       1572418048 |            1572418048 | 2019-10-30 14:47:28 |
+------------------+-----------------------+---------------------+
1 row in set (0.11 sec)

from_unixtime与unix_timestamp为互反函数

mysql> select from_unixtime(1572418048);
+---------------------------+
| from_unixtime(1572418048) |
+---------------------------+
| 2019-10-30 14:47:28       |
+---------------------------+
1 row in set (0.10 sec)

4、返回UTC日期和时间的函数

mysql> select utc_date(),utc_time();
+------------+------------+
| utc_date() | utc_time() |
+------------+------------+
| 2019-10-30 | 06:56:44   |
+------------+------------+
1 row in set (0.09 sec)

5、获取月份的函数

MONTH(date)函数返回对应的月份,MONTHNAME(date)返回月份对应的英文

mysql> select month('2019-10-30'),monthname('2019-10-30');
+---------------------+-------------------------+
| month('2019-10-30') | monthname('2019-10-30') |
+---------------------+-------------------------+
|                  10 | October                 |
+---------------------+-------------------------+
1 row in set (0.09 sec)

6、获取星期的函数

dayname(d)函数返回d对应的工作日的英文名称

dayofweek(d)返回日期所在周的索引

mysql> select dayname('2019-10-30'),dayofweek('2019-10-30');
+-----------------------+-------------------------+
| dayname('2019-10-30') | dayofweek('2019-10-30') |
+-----------------------+-------------------------+
| Wednesday             |                       4 |
+-----------------------+-------------------------+
1 row in set (0.10 sec)

7、获取星期数的函数

week(d,default_week_format)默认值是0,指定一周的第一天为周日,1则指定一周的第一天为周一

weekofyear(d)某天位于一年中的第几周,相当于week(d,3)

mysql> select week('2019-10-30'),weekofyear('2019-10-30');
+--------------------+--------------------------+
| week('2019-10-30') | weekofyear('2019-10-30') |
+--------------------+--------------------------+
|                 43 |                       44 |
+--------------------+--------------------------+
1 row in set (0.13 sec)

mysql> select week('2019-10-30',1),weekofyear('2019-10-30');
+----------------------+--------------------------+
| week('2019-10-30',1) | weekofyear('2019-10-30') |
+----------------------+--------------------------+
|                   44 |                       44 |
+----------------------+--------------------------+
1 row in set (0.09 sec)

8、获取天数的函数

dayofyear(d) 一年中的第几天

dayofmonth(d)一个月中的第几天

mysql> select dayofmonth('2019-10-30'),dayofyear('2019-10-30');
+--------------------------+-------------------------+
| dayofmonth('2019-10-30') | dayofyear('2019-10-30') |
+--------------------------+-------------------------+
|                       30 |                     303 |
+--------------------------+-------------------------+
1 row in set (0.09 sec)

9、获取年份、季度、小时、分钟和秒钟的函数

mysql> select year('2019-10-30 14:47:28'),quarter('2019-10-30 14:47:28'),minute('2019-10-30 14:47:28'),second('2019-10-30 14:47:28');
+-----------------------------+--------------------------------+-------------------------------+-------------------------------+
| year('2019-10-30 14:47:28') | quarter('2019-10-30 14:47:28') | minute('2019-10-30 14:47:28') | second('2019-10-30 14:47:28') |
+-----------------------------+--------------------------------+-------------------------------+-------------------------------+
|                        2019 |                              4 |                            47 |                            28 |
+-----------------------------+--------------------------------+-------------------------------+-------------------------------+
1 row in set (0.11 sec)

10、时间和秒钟转换的函数

time_to_sec(time)

sec_to_time(second)

mysql> select time_to_sec('14:47:28'),sec_to_time(53248);
+-------------------------+--------------------+
| time_to_sec('14:47:28') | sec_to_time(53248) |
+-------------------------+--------------------+
|                   53248 | 14:47:28           |
+-------------------------+--------------------+
1 row in set (0.10 sec)

11、计算日期和时间的函数

date_add(date,interval expr type)

adddate(date,interval expr type)

date_sub(date,interval expr type)

subdate(date,interval expr type)

addtime(date,expr)

subtime(date,expr)

datediff() 两个日期的间隔天数

计算日期和时间的格式
type值 预期的expr格式
MICROSECOND MICROSECONDS
SECOND SECONDS
MINUTE MINUTES

HOUR

HOURS
DAY DAYS
WEEK WEEKS
MONTH MONTHS
QUARTER QUARTERS
YEAR YEARS

SECOND_MICROSECOND

'SECONDS.MICROSECONDS'
MINUTE_MICROSECOND 'MINUTES.MICROSECONDS'
MINUTE_SECOND 'MINUTES:SECONDS'
HOUR_MICROSECOND 'HOUR.MICROSECONDS'
HOUR_SECOND 'HOURS:MINUTES:SECONDS'
HOUR_MINUTE 'HOURS:MINUTES'
DAY_MICROSECOND 'DAYS:MICROSECONDS'
DAY_SECOND 'DAYS HOURS:MINUTES:SECONDS'
DAY_MINUTE 'DAYS HOURS:MINUTES'
DAY_HOUR 'DAYS HOURS'
YEAR_MONTH 'YEARS-MONTHS'

 举例:使用三个type值 day,second,year_month 其他类比

mysql> select date_sub('2019-10-30',interval 10 day),date_sub('2019-10-30 14:47:28',interval 10 second),date_add('2019-10-30 14:47:28',interval '1_1' year_month);
+----------------------------------------+----------------------------------------------------+-----------------------------------------------------------+
| date_sub('2019-10-30',interval 10 day) | date_sub('2019-10-30 14:47:28',interval 10 second) | date_add('2019-10-30 14:47:28',interval '1_1' year_month) |
+----------------------------------------+----------------------------------------------------+-----------------------------------------------------------+
| 2019-10-20                             | 2019-10-30 14:47:18                                | 2020-11-30 14:47:28                                       |
+----------------------------------------+----------------------------------------------------+-----------------------------------------------------------+

 addtime

mysql> select addtime('2019-10-30 14:47:28','1:1:1'),addtime('14:47:28','1:1:1');
+----------------------------------------+-----------------------------+
| addtime('2019-10-30 14:47:28','1:1:1') | addtime('14:47:28','1:1:1') |
+----------------------------------------+-----------------------------+
| 2019-10-30 15:48:29                    | 15:48:29                    |
+----------------------------------------+-----------------------------+
1 row in set (0.09 sec)

datediff

mysql> select datediff('2019-10-30 14:47:28','2019-09-23 10:27:18');
+-------------------------------------------------------+
| datediff('2019-10-30 14:47:28','2019-09-23 10:27:18') |
+-------------------------------------------------------+
|                                                    37 |
+-------------------------------------------------------+
1 row in set (0.10 sec)

12、将日期时间格式化的函数

date_format(date,format)

DATE_FORMAT时间日期格式
说明符 说明
%a 工作日的缩写名称(Sun...Sat)
%b 月份的缩写名称(Jan...Dec)
%c 月份、数字形式
%D 带有英语后缀的该月日期
%d 该月日期,数字形式
%e 该月日期,数字形式
%f 微妙
%H 以2位数表示24小时
%h 以2位数表示12小时
%i 分钟,数字形式
%j 一年中的天数
%k 以24小时表示时间
%l 以12小时表示时间
%M 月份名称,英文
%m 月份名称,数字形式
%p 上午AM 或者下午PM
%r 时间12小时制
%S,%s 以数字形式表示秒
%W 工作日名称,中文
%w 一周的工作日,0表示周日
%Y 四位数表示年份
%y 两位数表示年份
%% ‘%’文字字符

 

mysql> select date_format('2019-10-30 14:47:28','%a %b %c %d %M %m %Y %y');
+--------------------------------------------------------------+
| date_format('2019-10-30 14:47:28','%a %b %c %d %M %m %Y %y') |
+--------------------------------------------------------------+
| Wed Oct 10 30 October 10 2019 19                             |
+--------------------------------------------------------------+
1 row in set (0.08 sec)