• 【MySQL】内置函数——日期函数


    函数名称描述
    current_date()当前日期
    current_time()当前时间
    current_timestamp()当前时间戳
    date(datetime)返回datetime的日期部分
    date_add(date,interval d_value_type)在date中添加日期或时间。interval后的数值可以是:year,day,minute,second
    date_sub(date,interval d_value_type)在date中减去日期或时间。interval后的数值可以是:year,day,minute,second
    dateduff(date1,date2)计算两个日期的差,单位是天,date1-date2
    now()当前日期时间

    //显示当前日期
    mysql> select current_date();
    +----------------+
    | current_date() |
    +----------------+
    | 2023-10-09     |
    +----------------+
    //显示当前时间
    mysql> select current_time();
    +----------------+
    | current_time() |
    +----------------+
    | 22:37:21       |
    +----------------+
    //显示当前时间戳
    mysql> select current_timestamp();
    +---------------------+
    | current_timestamp() |
    +---------------------+
    | 2023-10-09 22:37:30 |
    +---------------------+
    //显示当前日期+时间
    mysql> select now();
    +---------------------+
    | now()               |
    +---------------------+
    | 2023-10-09 22:48:56 |
    +---------------------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28

    //加上13天
    mysql> select date_add('2013-10-10',interval 13 day);
    +----------------------------------------+
    | date_add('2013-10-10',interval 13 day) |
    +----------------------------------------+
    | 2013-10-23                             |
    +----------------------------------------+
    //减去14天
    mysql> select date_sub('2013-10-10',interval 14 day);
    +----------------------------------------+
    | date_sub('2013-10-10',interval 14 day) |
    +----------------------------------------+
    | 2013-09-26                             |
    +----------------------------------------+
    //计算两个日期相差的天数
    mysql> select datediff('2018-10-3','2012-9-1');
    +----------------------------------+
    | datediff('2018-10-3','2012-9-1') |
    +----------------------------------+
    |                             2223 |
    +----------------------------------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    //date获取datetime的日期部分
    mysql> select date(current_timestamp);
    +-------------------------+
    | date(current_timestamp) |
    +-------------------------+
    | 2023-10-09              |
    +-------------------------+
    
    mysql> select date(now());
    +-------------+
    | date(now()) |
    +-------------+
    | 2023-10-09  |
    +-------------+
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
  • 相关阅读:
    总结1057
    110道 MySQL面试题及答案 (持续更新)
    学生个人网页模板 学生个人网页设计作品 简单个人主页成品 个人网页制作 HTML学生个人网站作业设计代做
    关于c++中数据sqrt() 精度问题
    STM32 GPIO的几种工作模式
    从零开始实现lmax-Disruptor队列(一)RingBuffer与单生产者、单消费者工作原理解析
    Windows:Arm,我们不合适
    Python爬虫——scrapy-4
    蓝桥杯备赛第五篇(动态规划)
    微服务系统架构的演变
  • 原文地址:https://blog.csdn.net/m0_72563041/article/details/133719496