Skip to content

DATEDIFF()

函数说明

DATEDIFF() 函数返回两个日期之间的天数。

函数语法

> DATEDIFF(expr1,expr2)

参数释义

参数 说明
expr1, expr2 必要参数。expr1 和 expr2 参数是合法的日期或日期/时间表达式。只有值的日期部分参与计算。

示例

mysql> SELECT DATEDIFF('2007-12-31 23:59:59','2007-12-30');
+-------------------------------------------+
| datediff(2007-12-31 23:59:59, 2007-12-30) |
+-------------------------------------------+
|                                         1 |
+-------------------------------------------+
1 row in set (0.00 sec)

mysql> SELECT DATEDIFF('2010-11-30 23:59:59','2010-12-31');
+-------------------------------------------+
| datediff(2010-11-30 23:59:59, 2010-12-31) |
+-------------------------------------------+
|                                       -31 |
+-------------------------------------------+
1 row in set (0.00 sec)
create table t1(a INT,  b date);
insert into t1 values(1, "2012-10-11");
insert into t1 values(2, "2004-04-24");
insert into t1 values(3, "2008-12-04");
insert into t1 values(4, "2012-03-23");
insert into t1 values(5, "2000-03-23");
insert into t1 values(6, "2030-03-23");
insert into t1 values(7, "2040-03-23");

mysql> SELECT a, DATEDIFF('2022-10-9', b) from t1;
+------+------------------------+
| a    | datediff(2022-10-9, b) |
+------+------------------------+
|    1 |                   3650 |
|    2 |                   6742 |
|    3 |                   5057 |
|    4 |                   3852 |
|    5 |                   8235 |
|    6 |                  -2722 |
|    7 |                  -6375 |
+------+------------------------+
7 rows in set (0.01 sec)