MySQL DATEDIFF() Function
The DATEDIFF()
function in MySQL calculates the difference between two dates and returns the result as the number of days between them.
DATEDIFF(): Definition and Usage
DATEDIFF()
is a very useful function for working with dates and time intervals. It's frequently used to calculate durations, to filter data based on time ranges, or to perform other time-based analyses. The function always returns the difference in days; it doesn't return partial days or other time units.
Syntax
Syntax
DATEDIFF(date1, date2)
Parameter Values
Parameter | Description |
---|---|
date1, date2 |
The two dates you want to find the difference between. The order matters (date1 - date2). Both are required. |
Examples
Calculating the Difference Between Two Dates
This example finds the number of days between June 15th, 2017 and June 25th, 2017.
Syntax
SELECT DATEDIFF("2017-06-25", "2017-06-15");
Output
10
Calculating the Difference Between Two Datetimes
This example demonstrates that DATEDIFF()
works with datetime values; only the date portion is considered.
Syntax
SELECT DATEDIFF("2017-06-25 09:34:21", "2017-06-15 15:25:35");
Output
9
Calculating the Difference Across Months
Calculating the number of days between December 24th, 2016 and January 1st, 2017.
Syntax
SELECT DATEDIFF("2017-01-01", "2016-12-24");
Output
8