How to Compare two (2) Dates in JavaScript?

Discover the various methods of how to compare two dates in JavaScript.

This article provides a comprehensive guide working with dates and times in JavaScript, including examples and solutions for comparing two dates.

So keep reading to learn new insights on comparing dates in JavaScript that will enhance your skills!

What is Date object in JavaScript?

The Date object is a built-in object in JavaScript that represents a single moment in time.

It is based on a time value that is the number of milliseconds since midnight of January 1, 1970, UTC (Coordinated Universal Time).

The Date object provides methods for working with dates and times, such as getting the current date and time, setting the date and time, and comparing dates.

Here’s an example of how to create a new Date object representing the current date and time:

var currentDate = new Date();
console.log(currentDate);

This code creates a new Date object using the new Date() constructor. The currentDate variable now holds a reference to this Date object.

We can then use the console.log() method to print the current date and time to the console.

Output:

2023-08-02T07:21:41.899Z

Solution on how to Compare two (2) Dates in JavaScript?

The following are a few ways to compare two dates in JavaScript:

Solution 1: Use comparison operators

You can use the comparison operators <, >, <=, and >= to compare two Date objects.

Here’s an example of how to compare two dates in JavaScript using the Date object:

var date1 = new Date("August 02, 2022");
var date2 = new Date("August 02, 2023");

if (date1 < date2) { console.log("date1 is earlier than date2"); } else if (date1 > date2) {
console.log("date1 is later than date2");
} 

In this example, we create two Date objects representing two different dates. We can then use comparison operators such as <, > to compare the two dates.

Output:

date1 is earlier than date2

Here’s another example:

var date1 = new Date("2022-08-02");
var date2 = new Date("2023-08-02");

if (date1 < date2) { console.log("date1 is earlier than date2"); } else if (date1 > date2) {
console.log("date1 is later than date2");
} else {
console.log("date1 and date2 are the same");
}

Here, we create two Date objects representing two different dates and we use comparison operators such as <, >, and == to compare the two dates.

Output:

date1 is earlier than date2

Take note, using equality operators (==, !=, ===, or !==) does not work when you are comparing two Date objects.

This is because the equality operators in JavaScript do not function as expected when comparing two Date objects due to the comparison being based on reference rather than value.

Therefore, two Date variables are not considered equal unless they refer to the same Date object stored in memory.

To accurately compare two Date objects, you can convert them to a common format such as a timestamp or a date string. To avoid any problems with equality operators when comparing Date objects.

For example, you can use the getTime() method to get the time value of a Date object, then compare these time values using comparison operators.

Here’s an example:

var date1 = new Date("2023-08-02");
var date2 = new Date("2023-08-02");

if (date1.getTime() === date2.getTime()) {
console.log("date1 and date2 are equal");
} else {
console.log("date1 and date2 are not equal");
}

As you can see, we create two Date objects representing the same date.

Then, we use the getTime() method to get their time values in milliseconds and compare these values using the === operator.

Output:

date1 and date2 are equal

Solution 2: Use the getTime() method

You can also use the getTime() method to compare time values using comparison operators.

Here’s an example:

var date1 = new Date("2024-08-02");
var date2 = new Date("2023-08-02");

if (date1.getTime() < date2.getTime()) { console.log("date1 is earlier than date2"); } else if (date1.getTime() > date2.getTime()) {
console.log("date1 is later than date2");
} else {
console.log("date1 and date2 are the same");
}

Output:

date1 is later than date2

Solution 3: Use the valueOf() method:

The valueOf() method returns the primitive value of a Date object. You can use this method to compare two dates by comparing their primitive values.

Here’s an example:

var date1 = new Date("2023-08-02");
var date2 = new Date("2023-08-03");

if (date1.valueOf() < date2.valueOf()) {
    console.log("date1 is earlier than date2");
} else if (date1.valueOf() > date2.valueOf()) {
    console.log("date1 is later than date2");
} else {
    console.log("date1 and date2 are the same");
}

Solution 4: Use the toISOString() method

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 characters long: YYYY-MM-DDTHH:mm:ss.sssZ.

You can use this method to compare two dates by comparing their ISO strings.

Here’s an example:

var date1 = new Date("2023-08-04");
var date2 = new Date("2023-08-03");

if (date1.toISOString() < date2.toISOString()) {
    console.log("date1 is earlier than date2");
} else if (date1.toISOString() > date2.toISOString()) {
    console.log("date1 is later than date2");
} else {
    console.log("date1 and date2 are the same");
}

Output:

date1 is later than date2

How to compare a date with the current date in JavaScript

You can compare a date with the current date in JavaScript by creating a new Date object representing the current date and time.

And then comparing it with another Date object using comparison operators or methods such as getTime() or valueOf().

Here’s an example:

var currentDate = new Date();
var someDate = new Date("2023-08-01");

if (someDate < currentDate) { console.log("someDate is earlier than the current date"); } else if (someDate > currentDate) {
console.log("someDate is later than the current date");
} else {
console.log("someDate and the current date are the same");
}

In this example, we create a new Date object representing the current date and time using the new Date() constructor. We also create another Date object representing a specific date.

Then use comparison operators such as <, >, and == to compare the two dates.

Output:

someDate is earlier than the current date

Conclusion

In conclusion, this article provides a comprehensive guide on how to compare two dates in JavaScript.

Along with various methods, including using comparison operators, getTime() method, valueOf() method, and toISOString() method.

It emphasizes the importance of converting Date objects into a common format, such as timestamps or date strings, to ensure accurate and reliable comparisons.

We are hoping that this article provides you with enough information that help you understand on how to compare dates in JavaScript.

You can also check out the following article:

Thank you for reading itsourcecoders 😊.

Leave a Comment