How to compare two dates in javascript

Date comparison is a fundamental requirement for any application. Often when displaying data on a page, you need to display the latest record at the top and then the records that were created later.
For that, the records should be compared based on their date of creation. Javascript provides different ways to compare dates using its Date object and this article will discuss them with examples.


Method 1: Using relational operator comparison
Two date objects can directly be compared using <, >, ==, <=, >= and != operators. Behind the scenes, their values are compared and the result is determined. Example,

// create two date objects
let dateOne = new Date(2019, 10, 1);
let dateTwo = new Date(2015, 10, 1);
if(dateOne > dateTwo) {
   console.log('Later date: ' + dateOne);
} else {
   console.log('Later date: ' + dateTwo);
}

Above code creates two date objects using its constructor which accepts year, month and date in the given order and then compares them using greater than(>) operator and prints the date which came later.
Note that month is zero based, meaning 0 will be January and 11 will be December.
Output is

Later date: Fri Nov 01 2019 00:00:00 GMT+0530 (India Standard Time)

Remember that == operator will return true if both the dates are same and != operator will return true if both the date objects have different values.


Method 2: Using valueOf function
Create two date objects and compare them using the valueOf function. valueOf returns the number of milliseconds of the date object since January 1, 1970. Example,

// create two date objects
let dateOne = new Date(2019, 10, 1);
let dateTwo = new Date(2015, 10, 1);
if(dateOne.valueOf() > dateTwo.valueOf()) {
   console.log('Later date: ' + dateOne);
} else {
   console.log('Later date: ' + dateTwo);
}


Method 3: Using getTime function
Create two date objects as in the previous methods and compare them using the getTime function of date object. getTime also returns the number of milliseconds of this date object since January 1, 1970. Example,

// create two date objects
let dateOne = new Date(2019, 10, 1);
let dateTwo = new Date(2015, 10, 1);
if(dateOne > dateTwo) {
   console.log('Later date: ' + dateOne);
} else {
   console.log('Later date: ' + dateTwo);
}

Output will be

Later date: Fri Nov 01 2019 00:00:00 GMT+0530 (India Standard Time)


Compare a date against today
You can also compare a given date with the current date. Javascript’s Date object provides a function now() which returns the current date converted to milliseconds.
Thus, its value can be compared to the number of milliseconds in a date object using valueOf or getTime functions as shown in Methods 2 and 3 above. Example,

// create a custom date object
let dateOne = new Date(2019, 10, 1);
// get current date
let currentDate = Date.now()
// compare milliseconds in both dates
if(dateOne.getTime() > currentDate) {
   console.log('Later date: ' + dateOne);
} else {
   console.log('Later date: ' + dateTwo);
}

Output will be

Later date: Fri Nov 01 2019 00:00:00 GMT+0530 (India Standard Time)

This is because the date object is ahead of the current date at the time this article is written.

Hit the clap below if the article was useful for you.

0

Mark Your Impression

Close Menu