【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  |
+-------------+
相关推荐
l1t5 小时前
DeepSeek总结的DuckLake 架构深度剖析-2
数据库·架构
YOU OU5 小时前
Redis事务
数据库·redis·缓存
辰同学ovo6 小时前
从一条 SQL 到三个原则:MySQL 心智模型
sql·mysql·adb
数智化管理手记6 小时前
财务大数据怎么管住资金风险?财务大数据和财务数智化到底怎么结合?
大数据·网络·数据库·人工智能·数据挖掘
晓子文集6 小时前
Tushare接口文档:指数成分和权重(index_weight)
大数据·数据库·python·金融·量化投资
吴声子夜歌6 小时前
MongoDB 4.2——入门指南
数据库·mongodb
吴声子夜歌6 小时前
MongoDB 4.x——高可靠
数据库·mongodb
light blue bird6 小时前
组轴式执行引擎 ✅ 多线程调度组装组件
数据库·信息可视化·oracle·桌面端winform·多节点端·gdi图表绘制开发
码农学院7 小时前
Node.js MongoDB构建户外用品GEO优化内容管理平台
数据库·mongodb·node.js
吴声子夜歌7 小时前
MongoDB 4.x——治理经验
数据库·mongodb