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;Time is ignored
MySQL 8.1
SELECT DATEDIFF('2022-12-05 23:59:59', '2022-12-01 00:00:01') AS days;Nights per reservation
MySQL 8.1
SELECT id,
start_date,
end_date,
DATEDIFF(end_date, start_date) AS nights
FROM Reservations;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.