How can I calculate the difference between two dates?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Calculating the difference between two dates sounds simple, but the correct method depends on what "difference" means in your application. Counting days between two date-only values is different from measuring hours between timestamps, and both are different again from computing calendar months or years.
Subtract Date Objects for Exact Day Differences
When you only care about elapsed days, the simplest approach is usually to convert both values to date objects and subtract them.
In Python:
This works well for date-only arithmetic because Python returns a timedelta, which carries the exact day difference.
In JavaScript, date-only calculations are safer when done in UTC to avoid local time-zone surprises:
If you create local midnight dates instead, daylight saving transitions can make "one day" look like 23 or 25 hours.
Distinguish Calendar Differences from Elapsed Time
Suppose you want the difference between 2024-01-31 and 2024-02-29. In elapsed days, that is 29 days. In calendar months, many people would describe it as 0 full months and 29 days, not "one month."
That is why months and years should not be calculated by dividing day counts by 30 or 365. Calendar units have variable lengths.
If you need calendar-aware differences, use a library that understands calendar rules. In Python, dateutil.relativedelta is a common choice:
The point is not that one interpretation is always correct. The point is that the unit definition must match the business rule.
That question becomes even more important in reporting systems. "Days since signup," "full months elapsed," and "age in years" sound similar, but they are not calculated the same way.
Normalize Time Zones Before Comparing Timestamps
If your values include times and time zones, normalize them before subtraction. Comparing naive local times from different zones produces confusing and sometimes wrong results.
A good rule is:
- use date objects for day-based business logic
- use timezone-aware datetimes for clock-time elapsed duration
- use calendar-aware libraries for months and years
Once that distinction is clear, most date-difference bugs become design bugs rather than arithmetic bugs.
Common Pitfalls
- Dividing elapsed days by
30to get months or by365to get years. - Subtracting local JavaScript dates across daylight saving changes without using UTC.
- Mixing naive and timezone-aware datetime values.
- Forgetting to define whether the start date, end date, or both should be counted inclusively.
- Assuming a generic "difference between two dates" function can satisfy every reporting and business-rule interpretation.
Summary
- Date subtraction is easy once you define the unit you actually need.
- Use date objects and timedeltas for exact day differences.
- Use timezone-aware timestamps for elapsed time.
- Use calendar-aware libraries for months and years.
- Always define the business rule before writing the calculation.
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.