【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  |
+-------------+
相关推荐
FungLeo1 小时前
FengCMS · PHP+MySQL 升级实录 · 输出转义的度:面包屑双重编码、默认转义与 cms_html()信任声明
mysql·html·php·fengcms·输出转义
Decay__2 小时前
PostgreSQL 用户权限与安全 —— 角色体系、GRANT 授权、pg_hba.conf 认证、行级安全与审计
运维·数据库·sql·postgresql
StarsGreen2 小时前
Android VINTF 中 Device Manifest 与 Framework Manifest 详解
android
Escalating_xu3 小时前
【MySQL】索引原理(上):从数据页到 B+ 树,弄懂聚簇索引、回表与覆盖索引
数据库·mysql
FungLeo3 小时前
FengCMS ·PHP+MySQL升级实录 · 富文本清洗配置实战:白名单、URI scheme、禁 iframe/id、实例缓存
mysql·缓存·php·xss·fengcms
DataY轻量数据平台4 小时前
MongoDB 怎么做 CDC?不用 Debezium,Change Streams 就是它的“binlog”
数据库·mongodb
Name_NaN_None4 小时前
安卓手机远程控制电脑方法(Android/Windows) —— 「小白教程」
android·windows·智能手机·电脑·笔记本
恋猫de小郭4 小时前
Flutter Perfetto:Dart Heap Sampling 终于要接入 Android 原生内存分析了
android·前端·flutter
djjjx.4 小时前
Mysql数据库基础篇(十):从磁盘 IO 到 B+ 树:图解 MySQL 索引的底层演进、Page 结构、目录机制与 IO 优化原理
数据库·mysql
En^_^Joy5 小时前
LangChain消息系统全解析:从基础到实战(四)
数据库·microsoft·langchain