How to check if a Date is before or after another Date

Javascript Jeep🚙💨
3 min readMay 11, 2023

This blog post explains three methods for checking if a date is before or after another date in JavaScript.

Photo by Towfiqu barbhuiya on Unsplash

When working with dates in web development, checking whether one date is before or after another date is a common task. This is particularly important when creating event calendars, scheduling appointments or booking services. In JavaScript, comparing dates to determine order can be done in various ways. In this blog post, we will dive into the details of each method to help you determine which approach best suits your needs.

Comparison Operators

One way of checking if a date is before or after another date in JavaScript is to use the comparison operators. These operators include < (less than), > (greater than), <= (less than or equal to), and >= (greater than or equal to).

For example, to check if date1 is before date2, you can use the < operator as shown below:

let date1 = new Date('2022-01-01');
let date2 = new Date('2022-01-10');
if (date1 < date2) {
console.log('date1 is before date2');
} else {
console.log('date1 is after date2');
}

In this case, the output will be ‘date1 is before date2’, as January 1st, 2022 comes before January 10th, 2022.

Using getTime() Method

Another way to compare dates in JavaScript is to use the getTime() method. The getTime() method returns the number of milliseconds between midnight of January 1, 1970, and the specified date. We can use this method to compare two dates in milliseconds, and then check which one is greater.

For example, here’s how to check if date1 is before date2 using the getTime() method:

let date1 = new Date('2022-01-01');
let date2 = new Date('2022-01-10');
if (date1.getTime() < date2.getTime()) {
console.log('date1 is before date2');
} else {
console.log('date1 is after date2');
}

In this case, the output will be the same as before, as both methods are checking the same thing.

Using Moment.js Library

Moment.js is a popular JavaScript library that makes working with dates and times more straightforward. It provides an intuitive and streamlined interface for parsing, validating, manipulating, and formatting dates and times.

To use Moment.js, we need to include the library in our project and create a moment object for each date we want to compare. Then, we can use the isBefore() and isAfter() methods to compare the dates and determine their order.

For example:

let date1 = moment('2022-01-01');
let date2 = moment('2022-01-10');
if (date1.isBefore(date2)) {
console.log('date1 is before date2');
} else {
console.log('date1 is after date2');
}

Once again, this code will output ‘date1 is before date2’.

Conclusion

Checking if a date is before or after another date is a necessary task when working with dates in web development. In JavaScript, we have several methods to do this, such as using comparison operators like < and >, or using methods like getTime() and isBefore() provided by the Moment.js library. The choice of which method to use ultimately depends on the application’s specific requirements. By selecting the ideal approach, you can make your code more efficient, streamlined and user-friendly.

--

--