How to compare dates in JavaScript?

Comparing two dates in JavaScript can be tricky, especially if you’re not familiar with the built-in date objects and methods. People also often use some external libraries like moment.js or date-fns, but this is totally redundant. JavaScript’s native methods are quite enough to compare two dates. Whether you’re a beginner or an experienced developer, this guide will provide you with the knowledge you need to work with dates in JavaScript.

How to compare dates in JavaScript?

The most universal and efficient approach to comparing dates in JavaScript is converting them to the timestamps (number of milliseconds elapsed since 1 January 1970) and comparing two timestamps. We can do such a conversion with the use of Date object and its getTime() method.

const date1 = new Date().getTime() // or Date.now();
const date2 = new Date('03/24/2022').getTime();

if (date1 > date2) {
  console.log("Now is later than it was 03/24/2022");
}

In the first line, we are creating a new instance of date object – new Date(), which returns the current date when no attributes are passed. In the second line, we are passing date string as an attribute. This string is converted to date object. In both cases, we are using getTime() method, which returns number of milliseconds elapsed from 1 January 1970 to our date. Now, we can compare these two numbers. Number of milliseconds to today will be bigger than the number of milliseconds to 3 March 2022.

It is worth noticing that timestamp in JavaScript means the number of milliseconds since 1 January 1970, and timestamp in other programming languages are often the number of seconds since this date. It is worth knowing that to avoid issues, for example when we are trying to convert timestamp returned by PHP to date in JavaScript.

How to compare specific date values (year or month)?

The date object has a lot of built-in methods to work with the dates. The ones of these methods are getFullYear() or get getMonth() which, as the name suggests, returns the year or month of the passed date (you can find the full list of available methods here).

If you want to compare two years of a specific date, you can use the below piece of code:

const date1 = new Date('01/24/2023').getFullYear();
const date2 = new Date('03/25/2022').getFullYear();

if (date1 > date2) {
  console.log("2023 is greater than 2022");
}

And accordingly when you want to compare months of the date, use this code:

const date1 = new Date('03/28/2022').getMonth();
const date2 = new Date('02/20/2022').getMonth();

if (date1 > date2) {
  console.log("March is later than February");
}

It is important to know, that months in JavaScript are counted from 0 to 11. If you will try to use the above code, you can notice that if statement checks if 2 is greater than 1 (2 represents March, and 1 represents February).

Now you are able to compare two dates in plain JavaScript, without using external libraries that are totally useless for operations like this.

Leave a Reply

Your email address will not be published. Required fields are marked *