How To Compare Dates in JavaScript?

How To Compare Dates in JavaScript?

Comparing dates is important when you are working with different date formats and time zones in JavaScript. In this blog, we will explore the various methods of comparing two dates using JavaScript.

Table of Contents:

Understanding JavaScript Date Object

JavaScript is used to perform multiple tasks. It provides you with a built-in Date object to work with dates and times. You can easily get the date and time using the Date object in JavaScript.

Example:

Javascript

Output:

Understanding JavaScript Date Object output

Explanation: In this example, you’re using the Date() Constructor to print the date on the console. You can also get the present date by using the .getDate() method.

Basic Methods to Compare Dates

In JavaScript, there are multiple ways to compare dates. Here are some of the ways that are mostly used for comparing dates in JavaScript.

Method 1: Using getTime() Method

The getTime() method of the Date object returns the number of milliseconds from January 1, 1970. Thus, it is the best and simplest way to compare dates.

Example:

Javascript

Output:

Using getTime() Method output

Explanation: In this example, we use the .getTime() method of the Date() object in JavaScript to compare the dates. It returns the number of milliseconds from January 1, 1970. Thus, you are just comparing these milliseconds and getting the output on the console “date1 is earlier than date2”.

Method 2: Using Relational Operators

In JavaScript, Date objects store values as numbers; thus, you can also use relational operators ( >, <, >=, <=, !=, ==) for comparisons.

Example:

Javascript

Output:

Using Relational Operators Output

Explanation: In this example, we’re directly using relational operators to compare the dates in JavaScript. Internally, Date objects store values as numbers.

Method 3: Using External Libraries.

JavaScript also provides you with some external libraries like moment.js and date-fns for comparing dates. Let’s discuss them one by one:

Using moment.js

moment.js is an external library that you can use to make comparisons between two dates in JavaScript. It provides various methods like isBefore(), isAfter(), and isSame() for date comparison. It is depreciated but still used in the old projects.

Example:

Javascript

Explanation: In this example, we are using moment.js to compare the two dates. d1.isAfter(d2) will print true as output because ‘2025-03-20’ comes after ‘2025-01-25’. Similarly, d1.isBefore(d2) and d1.isSame(d2) produce false.

Using date-fns (modern approach)

It is the modern alternative approach that you can use in place of moment.js. It is also helpful to compare dates in JavaScript.

Example:

Javascript

Explanation: In this example, we’re using the date-fns library instead of the moment.js. It also offers some methods like isBefore(), isAfter(), and isEqual() to compare the dates. It can give output in the form of true or false.

Comparing Dates With Different Formats

Sometimes, dates come in different formats, like 2025-03-21 and March 21, 2025. A comparison between the dates of different formats is challenging. Thus, it is better to convert them into a standard format before comparison.

Example:

Javascript

Output:

Comparing Dates With Different Formats Output

Explanation: In this example, we’re first converting the date strings into a standard format using the Date object, and you can perform the comparison between them using relational operators.

Handling Time Zones in Date Comparison

Handling different time zones makes the date comparison a little bit difficult. For Example, if one date string is stored in UTC format and the other is stored in local time-based format, then making a comparison between these two date strings of different formats may generate inaccurate results. Here is the way to solve this:

Example:

Javascript

Output:

Handling Time Zones in Date Comparison Output

Explanation: JavaScript automatically converts the time zones into UTC format. Thus, the value of d1 is March 20, 2025, 00:00:00 UTC, and the value of d2 is March 20, 2025, 00:00:00 UTC+2, but JavaScript converts it to March 19, 2025, 22:00:00 UTC. So, the dates are different, and the output is printed on the console.

How To Compare Only Date (Ignoring Time)?

If you want to compare the dates only, then it’s good to reset time components (hours, minutes, seconds,  milliseconds) to (0,0,0,0) before comparison.

Example:

Javascript

Output:

How To Compare Only Date Output

Explanation: Here, we are comparing dates using JavaScript. If the dates are equal, the output is true, otherwise, the output is false. The compareDate(date1, date2) function takes two dates as arguments and resets its time component to (0,0,0,0), and then makes the comparison.

Real-World Use Cases

Here are some of the real-world use cases where the date comparison is found to be useful:

  • Booking System: For checking room availability based on the check-in/check-out dates.
  • Event Management: For sorting events according to the upcoming dates.
  • Subscription Services: Checking Subscription renewal and their expiration date.

Best Practices For Comparing Two Dates In JavaScript

  • Use the getTime() method for a comparison.
  • Use the libraries for handling more difficult comparisons.
  • Normalize time to midnight (0,0,0,0) when there is a need to compare only dates.
  • Always handle time zones when working with international dates.

Conclusion

Comparing dates in JavaScript is important and used in many real-world applications like Booking Systems, Subscription Models, etc. You can use methods like the getTime(), relational operators, or external libraries. for comparing two dates in JavaScript. Choosing the right approach helps you get the best results.

For practicing interview questions in JavaScript, you can follow the Intellipaat’s JavaScript Interview Questions blog.

How to Compare Dates in JavaScript – FAQs

1. How do you compare two equal dates in JavaScript?

You can compare two equal dates in JavaScript using the .getTime() method or by using relational operators.

2. How do you compare two dates in JavaScript Moment?

In JavaScript, the moment.js library provides you the methods like isSame(), isAfter(), and isBefore() to compare the dates.

3. How do I get the current date in JavaScript?

To get the current date in JavaScript, you can use the Date object.

Example:

Javascript
4. Can we compare the two date strings?

Yes, it is possible to compare two date strings in JavaScript. You can do that by using the Date object, relational operators, or external libraries like moment.js and date-fns.

5. How do you check if two things are equal in JavaScript?

You can use the (===) operator to check whether two things are equal or not in JavaScript. It will return either true or false.

About the Author

Technical Research Analyst - Full Stack Development

Kislay is a Technical Research Analyst and Full Stack Developer with expertise in crafting Mobile applications from inception to deployment. Proficient in Android development, IOS development, HTML, CSS, JavaScript, React, Angular, MySQL, and MongoDB, he’s committed to enhancing user experiences through intuitive websites and advanced mobile applications.

Full Stack Developer Course Banner