MySQL DATEDIFF() Function
MySQL DATEDIFF(): Calculate Day Differences Between Two Dates
The DATEDIFF() function calculates the number of days between two date or datetime values by subtracting the second date from the first (expr1 - expr2). Importantly, DATEDIFF() completely ignores the time portion of both inputs.
Operations teams use DATEDIFF() to measure shipping lead times, overdue invoice days, and customer retention intervals.
Syntax
DATEDIFF() takes exactly two arguments in reverse chronological order compared to some other SQL dialects:
DATEDIFF(expr1, expr2)
expr1: The endingDATEorDATETIMEexpression.expr2: The startingDATEorDATETIMEexpression.
The return value is a signed integer (expr1 - expr2 in days). If expr1 is earlier than expr2, the result is negative.
Practical Example
Suppose you want to measure how many days passed between order_date and shipped_at in demo_orders, even when shipped_at includes clock times.
SELECT
order_id,
order_date,
shipped_at,
DATEDIFF(shipped_at, order_date) AS days_to_ship
FROM demo_orders
WHERE order_id IN (301, 302, 303);
| order_id | order_date | shipped_at | days_to_ship |
|---|---|---|---|
| 301 | 2026-05-01 | 2026-05-01 23:45:00 | 0 |
| 302 | 2026-05-02 | 2026-05-05 08:10:00 | 3 |
| 303 | 2026-05-04 | NULL | NULL |
Notice order 301: even though it shipped almost 24 hours later, both dates fall on 2026-05-01, so DATEDIFF() returns 0.
Common Mistakes & NULL Behavior
Avoid these common traps when using DATEDIFF():
- Time portions are ignored:
DATEDIFF('2026-05-02 00:01:00', '2026-05-01 23:59:00')returns1because only the calendar dates (2026-05-02minus2026-05-01) are compared, even though only two minutes elapsed. - Argument order matters: Writing
DATEDIFF(start_date, end_date)produces a negative number. Put the later date first to get positive durations. - Only two arguments allowed: Unlike SQL Server, MySQL
DATEDIFF()does not accept a unit likeMONTHorHOUR. UseTIMESTAMPDIFF(unit, start, end)for other units. NULLor bad dates yieldNULL: If either argument isNULLor invalid (like'2026-02-30'),DATEDIFF()returnsNULL.
Frequently Asked Questions
How do I calculate the difference in hours or months instead of days?
Use TIMESTAMPDIFF(HOUR, start_dt, end_dt) or TIMESTAMPDIFF(MONTH, start_dt, end_dt). Unlike DATEDIFF(), TIMESTAMPDIFF() takes the unit first and puts the start date before the end date.
Why did DATEDIFF() return a negative integer?
The first argument (expr1) was chronologically earlier than the second argument (expr2). Swap the two arguments if you always want positive future offsets, or wrap the call in ABS().
Does DATEDIFF() account for leap years automatically?
Yes. DATEDIFF('2024-03-01', '2024-02-28') accurately returns 2 days because 2024 includes February 29.
Unlock Your
Full Potential.
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Try it Yourself
Experiment with the code from this lesson in our interactive playground.