mysql时间与日期函数

获取日期时间
函数 功能
curdate(),current_date() 返回当前日期,只包含年月日
curtime(), current_time() 返回当前时间,只包含时分秒
now(), sysdate(),current_timestamp(),localtime(),localtimestamp() 返回当前系统日期和时间
mysql> select curdate(), current_date() ;
±-----------±---------------+
| curdate() | current_date() |
±-----------±---------------+
| 2022-06-19 | 2022-06-19 |
±-----------±---------------+
1 row in set (0.00 sec)

mysql> select curtime(),current_time();
±----------±---------------+
| curtime() | current_time() |
±----------±---------------+
| 20:27:08 | 20:27:08 |
±----------±---------------+
1 row in set (0.00 sec)

mysql> select now(), sysdate(),current_timestamp(),localtime(),localtimestamp();
±--------------------±--------------------±--------------------±--------------------±--------------------+
| now() | sysdate() | current_timestamp() | localtime() | localtimestamp() |
±--------------------±--------------------±--------------------±--------------------±--------------------+
| 2022-06-19 20:40:15 | 2022-06-19 20:40:15 | 2022-06-19 20:40:15 | 2022-06-19 20:40:15 | 2022-06-19 20:40:15 |
±--------------------±--------------------±--------------------±--------------------±--------------------+
1 row in set (0.00 sec)

mysql> select utc_date(),utc_time();
±-----------±-----------+
| utc_date() | utc_time() |
±-----------±-----------+
| 2022-06-19 | 12:42:49 |
±-----------±-----------+
1 row in set (0.00 sec)
日期与时间戳的转换
函数 功能
unix_timestamp 以unix时间戳的形式返回当前时间
unix_timestamp(date) 将时间date以unix时间戳的形式返回
from_unixtime(timestamp) 将unix时间戳转为普通格式的时间
mysql> select unix_timestamp();
±-----------------+
| unix_timestamp() |
±-----------------+
| 1655647272 |
±-----------------+
1 row in set (0.00 sec)

mysql> select unix_timestamp(‘2022-06-19 22:22:22’);
±--------------------------------------+
| unix_timestamp(‘2022-06-19 22:22:22’) |
±--------------------------------------+
| 1655648542 |
±--------------------------------------+
1 row in set (0.00 sec)
mysql> select unix_timestamp(curdate());
±--------------------------+
| unix_timestamp(curdate()) |
±--------------------------+
| 1655568000 |
±--------------------------+
1 row in set (0.00 sec)

mysql> select from_unixtime(unix_timestamp(curdate()));
±-----------------------------------------+
| from_unixtime(unix_timestamp(curdate())) |
±-----------------------------------------+
| 2022-06-19 00:00:00 |
±-----------------------------------------+
1 row in set (0.00 sec)
获取月份、星期、星期数、天数等函数
函数 功能
year(date),month(date),day(date) 返回具体的日期值
hour(time),minute(time),second(time) 返回具体的时间值
monthname(date) 返回月份:January,…
datename(date) 返回星期几:MONDAY,TUESDAY…SUNDAY
weekday(date) 返回周几,周一是0
quarter(date) 返回日期对应的季度,范围是1~4
week(date),weekofyear(date) 返回一年中的第几周
dayofyear(date) 返回一年中的第几天
dayofmonth(date) 返回日期位于所在月份的第几天
dayofweek(date) 返回周几,周日是1
mysql> select curdate();
±-----------+
| curdate() |
±-----------+
| 2022-06-19 |
±-----------+
1 row in set (0.00 sec)

mysql> select year(curdate()), month(curdate()), day(curdate());
±----------------±-----------------±---------------+
| year(curdate()) | month(curdate()) | day(curdate()) |
±----------------±-----------------±---------------+
| 2022 | 6 | 19 |
±----------------±-----------------±---------------+
1 row in set (0.00 sec)

mysql> select hour(curtime()),minute(curtime()),second(curtime());
±----------------±------------------±------------------+
| hour(curtime()) | minute(curtime()) | second(curtime()) |
±----------------±------------------±------------------+
| 22 | 19 | 19 |
±----------------±------------------±------------------+
1 row in set (0.00 sec)

mysql> select monthname(curdate()),dayname(curdate());
±---------------------±-------------------+
| monthname(curdate()) | dayname(curdate()) |
±---------------------±-------------------+
| June | Sunday |
±---------------------±-------------------+
1 row in set (0.00 sec)

mysql> select weekday(curdate()),quarter(curdate());
±-------------------±-------------------+
| weekday(curdate()) | quarter(curdate()) |
±-------------------±-------------------+
| 6 | 2 |
±-------------------±-------------------+
1 row in set (0.00 sec)

mysql> select week(curdate()),weekofyear(curdate());
±----------------±----------------------+
| week(curdate()) | weekofyear(curdate()) |
±----------------±----------------------+
| 25 | 24 |
±----------------±----------------------+
1 row in set (0.00 sec)

mysql> select dayofyear(curdate()),dayofmonth(curdate()),dayofweek(curdate());
±---------------------±----------------------±---------------------+
| dayofyear(curdate()) | dayofmonth(curdate()) | dayofweek(curdate()) |
±---------------------±----------------------±---------------------+
| 170 | 19 | 1 |
±---------------------±----------------------±---------------------+
1 row in set (0.00 sec)
日期的操作函数
函数 作用
extract(type from date) 返回指定日期中的特定部分,type指定返回的值
type的取值及含义

type 含义
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 返回年和月
mysql> select extract(day from now()), extract(year_month from now());
±------------------------±-------------------------------+
| extract(day from now()) | extract(year_month from now()) |
±------------------------±-------------------------------+
| 19 | 202206 |
±------------------------±-------------------------------+
1 row in set (0.30 sec)
时间和秒钟转换的函数
函数 功能
time_to_sec(time) 将time转化为秒并返回结果值
sec_to_time(econds) 将seconds秒数转化为包含小时、分钟和秒的时间
mysql> select time_to_sec(now());
±-------------------+
| time_to_sec(now()) |
±-------------------+
| 82180 |
±-------------------+
1 row in set (0.00 sec)

mysql> select sec_to_time(82180);
±-------------------+
| sec_to_time(82180) |
±-------------------+
| 22:49:40 |
±-------------------+
1 row in set (0.00 sec)
计算日期和时间的函数
函数 功能
date_add(datetime,interval expr type),adddate(date,interval expr type) 返回与给定的日期相差interval时间段的日期时间
date_sub(datetime,interval expr type),subdate(date,interval expr type) 返回与给定的日期相差interval时间段的日期时间
mysql> select date_add(now(),interval 1 day),adddate(now(),interval 1 day);
±-------------------------------±------------------------------+
| date_add(now(),interval 1 day) | adddate(now(),interval 1 day) |
±-------------------------------±------------------------------+
| 2022-06-20 22:57:23 | 2022-06-20 22:57:23 |
±-------------------------------±------------------------------+
1 row in set (0.00 sec)

mysql> select date_sub(now(),interval 1 day),subdate(now(),interval 1 day);
±-------------------------------±------------------------------+
| date_sub(now(),interval 1 day) | subdate(now(),interval 1 day) |
±-------------------------------±------------------------------+
| 2022-06-18 22:59:33 | 2022-06-18 22:59:33 |
±-------------------------------±------------------------------+
1 row in set (0.00 sec)
日期的格式化及解析
函数 功能
date_format(date, fmt) 按照字符串fmt格式化日期date值
time_format(time, fmt) 按照字符串fmt格式化日期time值
get_format(date_type, format_type) 返回日期字符串的显示格式
str_to_date(str, fmt) 按照字符串fmt将str解析为日期
fmt

格式符 说明 格式符 说明
%Y 4位年份 %y 2位年份
%M 月名表示月份(January,…) %m 数字表示月份(01,02,03,…)
%b 缩写的月名(Jan.,Feb.,…) %c 数字表示月份(1,2,3,…)
%D 英文后缀表示月中的天数(1st,2nd,3rd,…) %d 两位数字表示月中的天数(01,02,…)
%e 数字形式表示月中的天数
%H 两位数字表示小时,24小时制(01,02,…) %h、%I 两位数字表示小时,12小时制(01,02,…)
%k 数字形式的小时,24小时制(1,2,3,…) %l 数字形式表示小时,12小时制(1,2,3,…)
%i 两位数字表示分钟(00,01,02,…)
%S、%s 两位数字表示秒(00,01,02,…)
%W 一周中的星期名称(Sunday,…) %a 一周中的星期缩写(Sun.,Mon.,…)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值