EN
MySQL - DATEDIFF() function example
0
points
In this article, we would like to show you DATEDIFF()
function example in MySQL.
Quick solution:
-- SELECT DATEDIFF('to_date','from_date');
SELECT DATEDIFF('YYYY-MM-DD','YYYY-MM-DD');
or
-- SELECT DATEDIFF('to_date','from_date');
SELECT DATEDIFF('YYYY-MM-DD hh:mm:ss','YYYY-MM-DD hh:mm:ss');
Where:
YYYY
- four-digit year format, e.g.2021
,MM
- month number (counted from01
to12
),DD
- day number (from01
to31
- depending on month),hh
- hour number (from00
to24
),mm
- minute number (from00
to59
),ss
- second number (from00
to59
).
DATEDIFF()
function returns the number of days between two date or datetime values.
Note:
It doesn't take time (
HH:MM:SS
) into account.
Practical example
In this example, we will calculate the difference between two dates using DATEDIFF()
function.
Queries:
SELECT DATEDIFF('2021-01-08','2021-01-05') AS 'data difference';
SELECT DATEDIFF('2021-01-08 09:35:27','2021-01-05 11:22:55') AS 'data difference';
Result:
Note:
Notice that both of the above queries give the same result (difference between
2021-01-08
and2021-01-05
-3
days). They don't take time into account.