mysql关于时间的一些函数

获取当前的日期和时间

# 查看当前日期和时间
select now();                # 2022-09-24 09:29:05
# 下面这三个函数, 可以不加括号, 也可以加上括号
select current_date, current_time, current_timestamp ;
# 2022-09-24	         09:34:00	2022-09-24 09:34:00

从时间中获取日期

select 
    date(current_timestamp)     # 2022-09-24
    , year(current_timestamp)   # 2022
    , month(current_timestamp)  # 9
    , day(current_timestamp()); # 24

日期的加减

select current_date();  # 2022-09-24
select date_add(current_date(), interval 1 day);  # 2022-09-25
select date_add(current_date(), interval 1 month); #  2022-10-24
select date_sub(current_date(), interval 1 day); #  2022-09-23
select date_sub(current_date(), interval 1 year); # 2021-09-24
select date_sub(current_date(), interval -1 month); # 2022-10-24

日期差和时间差的计算

select datediff('2022-09-27', '2022-09-23'); # 4
select timediff('2022-09-27 11:20:10', '2022-09-27 10:10:00'); # 01:10:10

# 注意后面两个参数是前小后大, 和datediff不同
select timestampdiff(day, '2022-09-20', '2022-09-23') ; # 3
select timestampdiff(hour, '2022-09-20 10:20:30', '2022-09-20 12:30:30') ; 
# 2
select timestampdiff(second, '2022-09-20 10:20:30', '2022-09-20 12:30:30') ; 
# 7800

字符串和日期的互换

# 字符串-> 日期
select str_to_date('20220927', '%Y%m%d'); 
# 2022-09-27 第二个参数是对第一个参数的格式解释

select str_to_date('2022/9/27', '%Y/%m/%d'); # 2022-09-27 
select str_to_date('2022年9月27日', '%Y年%m月%d日'); # 2022-09-27 
# 日期-> 字符串
select date_format(current_timestamp(), '%Y%m%d'); # 20220924
select date_format(current_timestamp(), '%Y年%m月%d日'); # 2022年09月24日

时间戳和日期的互换

select unix_timestamp(); # 1663985175
# 日期或者时间字符串 -> 时间戳
select unix_timestamp('2022-09-20 10:20:30'); # 1663640430
select unix_timestamp('2022-09-20'); # 1663603200
# 时间戳 -> 日期
select from_unixtime(1663640430);  # 2022-09-20 10:20:30

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
MySQL 5.7提供了丰富的时间函数来处理日期和时间。根据官方文档,一些常用的时间函数包括: 1. 获取当前日期和时间:使用NOW()、CURRENT_TIMESTAMP()、SYSDATE()或CURRENT_TIMESTAMP函数来获取当前的日期时间值。 2. 获取当前时间:可以使用CURTIME()、CURRENT_TIME()或TIME()函数来获取当前的时间。 3. 时间加减间隔:使用DATE_ADD()函数可以在日期或时间上添加一个间隔。可以使用DATE_SUB()函数在日期或时间上减去一个间隔。 4. 日期和时间的格式化:使用DATE_FORMAT()函数可以将日期和时间值格式化为特定的格式。 5. 提取时间部分:使用TIME()函数可以提取传递表达式的时间部分。 这些只是MySQL 5.7提供的一部分时间函数,更多的时间函数和用法可以参考MySQL官方文档中的详细说明。 引用: MySQL 关于时间函数的官方文档:https://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html MySQL 官方文档中的时间函数示例:1.1. 获取 YYYY-MM-DD HH:mm:ss SELECT NOW(),CURRENT_TIMESTAMP(),SYSDATE(),CURRENT_TIMESTAMP<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [MySQL中的时间函数](https://blog.csdn.net/weixin_38192427/article/details/123365010)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值