Function Reference

DATEDIFF

Returns the number of days between two dates.

MySQL 8.1
DATEDIFF(date1, date2)

Parameters

date1DATE
Date to subtract date2 from
date2DATE
Date to subtract

Return value

An integer number of days (date1 minus date2), negative if date1 is earlier; NULL if any argument is NULL

Examples

Difference in days

MySQL 8.1
SELECT DATEDIFF('2022-12-05', '2022-12-01') AS days;
days
4

Time is ignored

MySQL 8.1
SELECT DATEDIFF('2022-12-05 23:59:59', '2022-12-01 00:00:01') AS days;
days
4

Nights per reservation

MySQL 8.1
SELECT id,
	start_date,
	end_date,
	DATEDIFF(end_date, start_date) AS nights
FROM Reservations;
idstart_dateend_datenights
12018-11-13 12:00:002018-11-15 12:00:002
22018-10-09 12:00:002018-10-12 12:00:003
32018-12-29 12:00:002019-01-07 12:00:009
42019-02-01 12:00:002019-02-02 12:00:001
52019-02-02 12:00:002019-02-04 12:00:002
62020-03-21 09:00:002020-03-23 09:00:002
72020-03-21 10:00:002020-04-21 10:00:0031
82019-06-14 10:00:002019-06-24 10:00:0010
92019-05-11 10:00:002019-05-14 10:00:003
102019-11-04 10:00:002019-11-06 10:00:002

Details

The time in the arguments is ignored. To get the difference in hours, minutes or months, use TIMESTAMPDIFF. PostgreSQL has no such function: subtracting dates date1 - date2 gives the number of days, and AGE returns an interval in years, months and days.

See also

Where to learn