Get difference between 2 dates in JavaScript?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In JavaScript, calculating the difference between two dates is a common task encountered in many web development scenarios, from checking how long a user has been registered on a site to powering countdown clocks. To achieve this, JavaScript provides a straightforward approach leveraging the Date object. Below we will explore how to calculate the difference between two dates, various nuances associated with it, and some practical examples.
Understanding the Date Object in JavaScript
The JavaScript Date object represents a single moment in time in a platform-independent format. Date objects contain a Number that represents milliseconds since 1 January 1970 UTC.
Creating a date object can be done in various ways:
Calculating the Difference
To find the difference between two dates, you subtract one Date object from another, which gives you the difference in milliseconds. This result can then be converted into a more understandable unit like hours, days, or years.
Example:
Handling Time Zones
When working with dates, particularly across different locales or time zones, you might get unexpected results due to the local time zone being applied.
To deal with time zone issues, always use UTC dates when comparing:
Leap Years and Other Considerations
While calculating differences in days or larger units, consider variations like leap years or daylight saving changes. Months also vary in length, which can affect calculations when considering months or years difference.
Summary Table
The following table summarizes the conversions used to compute time differences from milliseconds:
| Unit | Division Factor |
| Milliseconds | 1 |
| Seconds | 1,000 |
| Minutes | 60,000 |
| Hours | 3,600,000 |
| Days | 86,400,000 |
Advanced Usage: Time Interval Functions
For a more human-friendly approach, rather than manual calculations, the Internationalization API provides Intl.RelativeTimeFormat. This API allows localized formatting of relative time phrases (e.g., "in 3 months" or "2 days ago").
Example:
Conclusion
Calculating the difference between two dates in JavaScript is straightforward once you handle the milliseconds conversion and consider time zone complexities. By using either the native Date methods or more sophisticated internationalization APIs, developers can handle even the most complex time-related tasks in their applications.
Related reading
- Get HTML source of WebElement in Selenium WebDriver using Python
- Get JavaScript object from array of objects by value of property
- Get protocol host name from URL
- Get request part after hash sign
- Get selected element's outer HTML
- Get size of a View in React Native
- Get the closest value for combinations of an array JS
- Get the current year in JavaScript
.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.