【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  |
+-------------+
相关推荐
老衲提灯找美女1 小时前
MySQL的增删改查功能合集
数据库·mysql·增删改查·增删改查详细用法
SelectDB2 小时前
Apache Doris 4.0.1 版本正式发布
数据库·apache
Doro再努力2 小时前
MySQL数据库07:分组查询与分类查询
数据库·mysql
Elastic 中国社区官方博客3 小时前
Elasticsearch:如何为 Elastic Stack 部署 E5 模型 - 下载及隔离环境
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
alexhilton3 小时前
在Jetpack Compose中创建CRT屏幕效果
android·kotlin·android jetpack
间彧3 小时前
SpringBoot + MyBatis-Plus + Dynamic-Datasource 读写分离完整指南
数据库·后端
静若繁花_jingjing4 小时前
NoSql数据库概念
数据库·nosql
间彧4 小时前
除了AOP切面,还有哪些更灵活的数据源切换策略?比如基于注解或自定义路由规则
数据库·后端
弥生赞歌4 小时前
Mysql作业四
数据库·mysql
间彧4 小时前
🏗️ Spring Boot 3实现MySQL读写分离完整指南
数据库