【MySQL用法】mysql日期时间函数总结(附带mysql官网介绍)

16 篇文章 1 订阅
15 篇文章 5 订阅

MySQL提供了大量丰富的函数,在MySQL的使用中经常会用到各种函数,本文记录的是常见的MySQL的日期与时间函数,主要用于处理日期和时间值。

MySQL 获得当前日期+时间 函数

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

SELECT now(), SYSDATE();

sysdate() 日期时间函数跟 now() 类似,区别在于:now() 在执行开始时值就得到了,得到的是一个不变的、恒定的时间, sysdate() 在函数执行时动态得到值。原文是这样的(SYSDATE() returns the time at which it executes. This differs from the behavior for NOW(), which returns a constant time that indicates the time at which the statement began to execute.)可查看官网的例子,如下:

mysql> SELECT NOW(), SLEEP(2), NOW();
+---------------------+----------+---------------------+
| NOW()               | SLEEP(2) | NOW()               |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:36 |        0 | 2006-04-12 13:47:36 |
+---------------------+----------+---------------------+

mysql> SELECT SYSDATE(), SLEEP(2), SYSDATE();
+---------------------+----------+---------------------+
| SYSDATE()           | SLEEP(2) | SYSDATE()           |
+---------------------+----------+---------------------+
| 2006-04-12 13:47:44 |        0 | 2006-04-12 13:47:46 |
+---------------------+----------+---------------------+

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

MySQL 获得当前时间戳 函数

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

SELECT current_timestamp, current_timestamp();


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

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

SELECT DATE_FORMAT(now(), '%Y-%m-%d %H:%i:%s'), TIME_FORMAT(now(),'%Y-%m-%d %H:%i:%s');

从结果可知,time_format(time,format)函数:只转换时间,不转换日期

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

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

select str_to_date('08/09/2019', '%m/%d/%Y'); -- 2019-08-09
select str_to_date('08/09/19' , '%m/%d/%y'); -- 2019-08-09
select str_to_date('08.09.2019', '%m.%d.%Y'); -- 2019-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2019 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2019-08-09 08:09:30

MySQL 日期、时间相减函数

DATEDIFF(date1,date2), TIMEDIFF(time1,time2)

MySQL DATEDIFF(date1,date2):两个日期相减 date1 - date2,返回天数。

SELECT DATEDIFF('2020-03-08', '2020-03-01'); -- 7

SELECT DATEDIFF('2020-03-01', '2020-03-08'); -- -7

 

MySQL TIMEDIFF(time1,time2):两个日期相减 time1 - time2,返回 time 差值。

SELECT TIMEDIFF('2020-03-25 17:51:25', '2020-03-25 17:50:20'); -- 00:01:05

SELECT TIMEDIFF('2020-03-25 17:50:20', '2020-03-25 17:51:25'); -- 00:01:05

注意:timediff(time1,time2) 函数的两个参数类型必须相同。

 

【参考资料】

1、Mysql官网介绍--12.6 Date and Time Functions(日期和时间函数): https://dev.mysql.com/doc/refman/8.0/en/date-and-time-functions.html#function_get-format

2、MySQL:日期函数、时间函数总结(MySQL 5.X):https://www.cnblogs.com/she27/archive/2009/01/16/1377089.html

3、Mysql基础,常用日期时间函数详解:https://baijiahao.baidu.com/s?id=1621234901544582551&wfr=spider&for=pc

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

No8g攻城狮

向每一个努力改变现状的你致敬!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值