Lesson

MySQL DATEDIFF() Function

10 min reading
Free Course

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 ending DATE or DATETIME expression.
  • expr2: The starting DATE or DATETIME expression.

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') returns 1 because only the calendar dates (2026-05-02 minus 2026-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 like MONTH or HOUR. Use TIMESTAMPDIFF(unit, start, end) for other units.
  • NULL or bad dates yield NULL: If either argument is NULL or invalid (like '2026-02-30'), DATEDIFF() returns NULL.

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.

Save Your Progress

Unlock Your
Full Potential.

Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.

Quick Access With

Enterprise-Grade Security Protocol

Try it Yourself

Experiment with the code from this lesson in our interactive playground.

Open Playground
MySQL Topic Hub

Stuck on this lesson?

Join our community of senior developers.

Ask in Forum