【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() 当前日期时间

sql 复制代码
//显示当前日期
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 |
+---------------------+

sql 复制代码
//加上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 |
+----------------------------------+

sql 复制代码
//date获取datetime的日期部分
mysql> select date(current_timestamp);
+-------------------------+
| date(current_timestamp) |
+-------------------------+
| 2023-10-09              |
+-------------------------+

mysql> select date(now());
+-------------+
| date(now()) |
+-------------+
| 2023-10-09  |
+-------------+
相关推荐
诸神黄昏EX24 分钟前
Android Build系列专题【篇四:编译相关语法】
android
盒马coding2 小时前
第19节-非规范化数据类型-Composite-types
数据库·postgresql
-雷阵雨-2 小时前
MySQL——桥梁JDBC
数据库·mysql·oracle
亿坊电商2 小时前
在PHP框架里如何进行数据库连接?
数据库·oracle·php
满昕欢喜3 小时前
SQL Server从入门到项目实践(超值版)读书笔记 28
数据库·sql·sqlserver
雨白3 小时前
优雅地处理协程:取消机制深度剖析
android·kotlin
楚韵天工3 小时前
宠物服务平台(程序+文档)
java·网络·数据库·spring cloud·编辑器·intellij-idea·宠物
leon_zeng03 小时前
更改 Android 应用 ID (ApplicationId) 后遭遇记
android·发布
JanelSirry4 小时前
MySQL分区表(PARTITION):水平分表示例 (基于用户ID哈希分表)不依赖第三方中间件
mysql·中间件·哈希算法
李白你好5 小时前
一款专业的多数据库安全评估工具,支持 **PostgreSQL、MySQL、Redis、MSSQL** 等多种数据库的后渗透操作
数据库·mysql·postgresql