Calculate difference between two datetimes in MySQL
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
MySQL gives you several ways to calculate the difference between two DATETIME values, and the right choice depends on the format you want back. Sometimes you want a count of hours or days. Other times you want a clock-style duration such as 01:45:30.
Use TIMESTAMPDIFF() for Numeric Units
TIMESTAMPDIFF() is the most direct choice when you want an integer difference in a specific unit such as seconds, minutes, hours, or days.
This returns 165. The function takes three arguments:
- the unit to measure in
- the start datetime
- the end datetime
Common units include SECOND, MINUTE, HOUR, DAY, MONTH, and YEAR.
Example with Table Data
Suppose you store job start and finish times:
This is usually the best pattern for reports, filtering, and aggregations because the result is numeric and easy to compare.
Use TIMEDIFF() for Clock-Style Output
If you want a time value rather than an integer count of units, use TIMEDIFF():
This returns a result such as 02:45:00. That can be useful for display, but it is less convenient than TIMESTAMPDIFF() when you need arithmetic or conditions such as "all rows longer than 90 minutes."
Negative Results and Argument Order
The order of arguments matters. MySQL subtracts the first datetime from the second one in TIMESTAMPDIFF(unit, start, end). If end is earlier than start, the result is negative.
That returns -6. Negative values are often useful because they preserve direction instead of hiding bad data.
Working with Dates Larger Than One Day
One subtlety is that TIMEDIFF() is presentation-oriented. If you are analyzing durations over long periods, numeric units from TIMESTAMPDIFF() are usually clearer. For example, a job that spans several days is easier to reason about as total minutes or total seconds than as a formatted time value.
When you need mixed formatting, calculate numerically first and then format deliberately in application code or in SQL with explicit arithmetic.
Nulls and Time Zone Considerations
If either datetime is NULL, the result is NULL. In real queries, it is common to protect against unfinished rows:
Also remember that DATETIME stores a calendar value without automatic time zone conversion. If your application mixes time zones, make sure both values were recorded consistently before comparing them.
Common Pitfalls
- Using
TIMEDIFF()when you really need an integer for filtering or aggregation. - Reversing the argument order and being surprised by negative results.
- Forgetting that
TIMESTAMPDIFF()returns whole units, not fractional ones. - Comparing datetimes recorded in inconsistent time zones.
- Ignoring
NULLend times in partially completed records.
Summary
- Use
TIMESTAMPDIFF()when you want a numeric difference in a chosen unit. - Use
TIMEDIFF()when you want a clock-style duration value. - Argument order matters and can produce negative results.
- Numeric durations are usually easier to filter, sort, and aggregate.
- Check for
NULLvalues and time zone consistency before trusting the result.

