How do I check the difference, in seconds, between two dates?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
To get the difference between two dates in seconds, the general pattern is always the same: parse or construct both dates, subtract them, and convert the resulting duration to seconds. The exact API changes by language, but the main complications are time zones, date parsing, and whether you want whole seconds or fractional seconds.
Python Example
In Python, subtracting two datetime values gives a timedelta, and total_seconds() converts that duration into seconds.
If you want an integer:
This is the most common and reliable Python approach.
JavaScript Example
In JavaScript, subtracting two Date objects gives the difference in milliseconds, so divide by 1000 to get seconds:
Because the result is based on milliseconds, this naturally supports fractional seconds too if the timestamps include them.
SQL Example
In SQL, the exact function depends on the database system. In PostgreSQL, subtracting timestamps gives an interval, and you can convert that to seconds:
In MySQL, TIMESTAMPDIFF is the common choice:
These functions are clearer than trying to manually subtract string values.
What About Time Zones
Time zone handling is where date-difference bugs usually come from. Two timestamps that look close together in local time may represent different instants if one is in UTC and the other is local time.
For example, in Python:
The important rule is consistency:
- compare two timezone-aware values in the same frame of reference
- or compare two naive values only when they truly represent the same local time system
Mixing aware and naive datetimes usually leads to incorrect results or explicit errors.
Whole Seconds vs Fractional Seconds
Some APIs return floating-point seconds, while others return whole numbers. Decide which one you need before converting.
For example, if sub-second precision matters in Python:
If you convert too early with int, you lose the fraction.
Negative Differences
Date subtraction can produce negative values when the earlier and later timestamps are reversed.
If you always want the absolute gap regardless of order:
That is often useful in elapsed-time comparisons, but not always correct for business logic that cares about ordering.
Common Pitfalls
The biggest pitfall is comparing strings instead of parsed date objects. Date arithmetic should happen on real date or time types, not on formatted text.
Another common issue is forgetting that JavaScript Date subtraction returns milliseconds, not seconds. Division by 1000 is required.
Time zones also cause subtle bugs. A local timestamp and a UTC timestamp may not be directly comparable in the way you expect unless you normalize them first.
Finally, decide whether you need whole seconds or fractional seconds. Truncating too early can silently lose precision.
Summary
- Parse both dates into real date or time objects first.
- Subtract the two values to get a duration, then convert that duration to seconds.
- Python uses
timedelta.total_seconds(). - JavaScript
Datesubtraction returns milliseconds, so divide by1000. - Handle time zones and fractional-second precision deliberately to avoid subtle errors.

