How to Compare Date Objects in JavaScript

In this blog, we discuss and compare 5 ways of comparing dates in JavaScript.

--

We deal with dates very often while building applications in JavaScript. It might be tedious to compare dates since there are many things to consider like day, month, year, time, etc.

JavaScript has the built-in object of date which allows us the comparison between two dates. Let’s discuss how to compare the two dates we have and see how it works. Note that this blog applies to only JavaScript’s native date objects.

1) Comparing two date objects directly:

We can compare two dates using comparison operators <or >as we do with other variables. This is the most straightforward solution to this problem.

d1 = new Date() // today's date
d2 = new Date(2021, 02, 05) // previous date
if (d1 > d2) {
console.log('d1 is greater than d2')
}
else {
console.log('d2 is greater than d1')
}
// result: d1 is greater than d2

NOTE: Note that ===and ==does not work with d1 and d2. Since date objects are well, objects. We cannot compare two objects directly since objects are reference types. But we can compare the properties.

d1 = new Date(2021, 02, 05)
d2 = new Date(2021, 02, 05)
d1 === d2 // false
d1 == d2 // false

2) Use .getTime()

Using .getTime() is one of the ways to compare if two dates are equal is one alternative way.

.getTime() return the numeric value of the given date as the number of milliseconds that have elapsed since January 1, 1970, 00:00:00 UTC.

d1 = new Date(2021, 02, 05)
d2 = new Date(2021, 02, 05)
if (d1.getTime() === d2.getTime()) {
console.log('Two dates are equal')
}
else {
console.log('Two dates are NOT equal')
}
// Result: Two dates are equal

Note that it works for <and >comparison operators too.

d1 = new Date('2022, 05, 10') 
d2 = new Date('2022, 05, 05')
if (d1.getTime() > d2.getTime()) {
console.log('d1 is ahead than d2') }
else {
console.log('d2 is ahead than d1')
}
// Result: d1 is ahead than d2

3) Using .toString():

We can use .toString() to convert the date to object to string and then compare it using == or === .

d1 = new Date('2022, 05, 05') 
d2 = new Date('2022, 05, 05')
if (d1.toString() === d2.toString())
{
console.log('Two dates are equal')
}
else
{
console.log('Two dates are NOT equal')
}

// Result: Two dates are equal

NOTE: Since we are converting an object to a string, comparison operators such as < and > do not work as expected here.

d1 = new Date('2022, 05, 10') 
d2 = new Date('2022, 05, 15')
if (d1.toString() > d2.toString()) {
console.log('d1 is ahead than d2')
} else {
console.log('d2 is ahead than d1')
}
// Result: d1 is ahead than d2
// > and < do not work when we are using .toString() method.

4) Using .valueOf:

The valueOf() method returns the primitive value of the specified object. This method also returns the elapsed number of milliseconds like .getTime(). Here’s how we can use it.

d1 = new Date(2021, 02, 05)
d2 = new Date(2021, 02, 05)
if (d1.valueOf() === d2.valueOf()) {
console.log('Two dates are equal')
}
else {
console.log('Two dates are NOT equal')
}
// Result: Two dates are equal

This works smoothly with < and > as well. So that’s an advantage.

d1 = new Date('2022, 05, 10') 
d2 = new Date('2022, 05, 15')
if (d1.valueOf() > d2.valueOf()) {
console.log('d1 is ahead than d2')
} else {
console.log('d2 is ahead than d1')
}
// Result: d2 is ahead than d1

5) Subtract two dates:

Subtract two dates and check if the result is going to be 0. If it is 0, then two dates are equal. Subtracting a - b gives you the difference between two dates in milliseconds.

d1 = new Date()
d2 = new Date()
if ((d2 - d1) === 0) {
console.log('Two dates are EQUAL')
}
else {
console.log('Two dates are NOT equal')
}
// Result: Two dates are EQUAL

We can compare two dates by subtracting two dates and checking if the result is positive or negative.

d1 = new Date('2022, 05, 10')
d2 = new Date('2022, 05, 15')
if (d1 - d2 > 0) {
console.log('d1 is ahead than d2')
}
else {
console.log('d2 is ahead than d1')
}
// Result: d2 is ahead than d1

Date only comparisons in JavaScript:

Sometimes there might be a situation where we do not want to consider the time component but we want to compare the date parts of the objects.

We might have to extract year, month and date using the help of .getFullYear(), .getMonth() and .getDate() respectively. These are built-in methods that are part of the JavaScript date object.

function isDateEqual(date1, date2) {
if (
date1.getFullYear() === date2.getFullYear() &&
date1.getMonth() === date2.getMonth() &&
date1.getDate() === date2.getDate()
) {
return "Two dates are Equal";
} else {
return "Two dates are not equal";
}
}
const date1 = new Date("2021, 05, 05");
const date2 = new Date("2021, 05, 06");
console.log(isDateEqual(date1, date2));// Result: Two dates are Equal

References:

Conclusion:

There we have it. We learnt 5 ways of comparing date objects. Comment below if you know of any other ways.

Thank you for reading 🙃

More content at PlainEnglish.io. Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord. Interested in Growth Hacking? Check out Circuit.

--

--