zoukankan      html  css  js  c++  java
  • MySQL 日期时间函数

    获得当前日期时间函数

    获得当前日期+时间:

    1.now()

    2.sysdate()

    mysql> select now(), sysdate(),sleep(1), now(),sysdate();
    +---------------------+---------------------+----------+---------------------+---------------------+
    | now()               | sysdate()           | sleep(1) | now()               | sysdate()           |
    +---------------------+---------------------+----------+---------------------+---------------------+
    | 2020-12-24 17:03:15 | 2020-12-24 17:03:15 |        0 | 2020-12-24 17:03:15 | 2020-12-24 17:03:16 |
    +---------------------+---------------------+----------+---------------------+---------------------+

    sysdate() 日期时间函数跟 now() 类似,不同之处在于:now() 在执行开始时值就得到了, sysdate() 在函数执行时动态得到值

    3.CURDATE()

    4.CURTIME()

    SELECT CURDATE(),CURTIME();-- 2020-12-25 和 17:33:57

    获得当前时间戳函数:

    1.current_timestamp

    2.current_timestamp()

    select current_timestamp, current_timestamp();-- 2020-12-24 17:05:45

    日期时间转换函数

    -- 日期转字符串
    select date_format('2020-12-24 12:10:01', '%Y-%m-%d %H:%i:%s'); -- 2020-12-24 12:10:01
    -- 时间转字符串
    select time_format('2020-12-24 12:10:01', '%H:%i:%s'); -- 12:10:01
    -- 字符串转日期
    select str_to_date('2020-12-24 12:10:01', '%Y-%m-%d %H:%i:%s'); -- 2020-12-24 12:10:01
    -- 日期转天数
    select to_days('2020-12-24'); -- 738148
    -- 天数转日期
    select from_days('738148'); -- 2020-12-24
    -- 时间转秒
    select time_to_sec('01:00:05'); -- 3605
    -- 秒转时间
    select sec_to_time(3605); -- '01:00:05'
    -- 拼凑日期
    select makedate(2001,32); -- '2001-02-01'
    -- 拼凑时间
    select maketime(12,15,30); -- '12:15:30'

    Unix 时间戳、日期转换

    select unix_timestamp(); -- 1608799459
    select unix_timestamp('2020-12-24 12:10:01'); -- 1608783001
    elect from_unixtime(1608783001); -- 2020-12-24 12:10:01
    select from_unixtime(1608783001,'%Y-%m-%d %H:%i:%s'); -- 2020-12-24 12:10:01

    日期时间计算函数

    set @dt = '2020-12-24 16:46:34';
    
    select date_add(@dt, interval 1 day); -- 2020-12-25 16:46:34
    select date_add(@dt, interval 1 hour); -- 2020-12-24 17:46:34
    select date_add(@dt, interval 1 minute); -- ...
    select date_add(@dt, interval 1 second);
    select date_add(@dt, interval 1 microsecond);
    select date_add(@dt, interval 1 week);
    select date_add(@dt, interval 1 month);
    select date_add(@dt, interval 1 quarter);
    select date_add(@dt, interval 1 year);
    
    select adddate(@dt, interval 1 day);-- 2020-12-25 16:46:34
    select addtime(@dt, 1);-- 2020-12-24 16:46:35
    select date_sub(@dt, interval '1 1:1:1' day_second);-- 2020-12-23 15:45:33
    
    select datediff('2020-12-24', '2020-11-24');-- 30
    select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00');-- 08:08:08
    select timediff('08:08:08', '00:00:00');-- 08:08:08
    
    select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00
    select timestampdiff(day ,'2002-05-01','2001-01-01'); -- 2008-08-09 08:00:00 

    时区转换函数

    select convert_tz('2008-08-08 12:00:00', '+08:00', '+00:00'); -- 2008-08-08 04:00:00
    select date_add('2008-08-08 12:00:00', interval -8 hour); -- 2008-08-08 04:00:00
    select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00
    select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00

    文章参考:https://www.cnblogs.com/ggjucheng/p/3352280.html

  • 相关阅读:
    OCP-1Z0-新051-61题版本-20
    OCP-1Z0-新051-61题版本-19
    OCP-1Z0-新051-61题版本-17
    OCP-1Z0-新051-61题版本-18
    OCP-1Z0-新051-61题版本-16
    OCP-1Z0-新051-61题版本-15
    OCP-1Z0-新051-61题版本-14
    OCP-1Z0-新051-61题版本-12
    OCP-1Z0-新051-61题版本-13
    OCP-1Z0-新051-61题版本-11
  • 原文地址:https://www.cnblogs.com/ooo0/p/14185103.html
Copyright © 2011-2022 走看看