Check if a Date is after another Date using JavaScript


Borislav Hadzhiev

Last updated: Jan 25, 2022


Photo from Unsplash

Check if a Date is after another Date using JavaScript #

To check if a date is after another date, compare the Date objects, e.g. date1 > date2. If the comparison returns true, then the first date is after the second, otherwise the first date is equal to or comes before the second.

function isAfter(date1, date2) { return date1 > date2; } const d1 = new Date('2022-08-20'); const d2 = new Date('2022-02-27'); console.log(isAfter(d1, d2)); // 👉️ true console.log(isAfter(d2, d1)); // 👉️ false

We created a reusable function that takes 2 Date objects as parameters and checks if the first date is after the second.

If you have a date string and need to create a Date object, pass the date string to the Date() constructor.

We are able to compare the dates because under the hood each date stores a timestamp - the number of milliseconds elapsed between the 1st of January 1970 and the given date.

const date = new Date('2022-06-22'); // 👇️ 16558560000000 console.log(date.getTime());
Each date stores a timestamp under the hood, so the default behavior is to compare the timestamps of the dates, even if you don't explicitly call the getTime() method on each date.

If the comparison returns true, then the timestamp of the second date is less than or equal to the timestamp of the first.

If the timestamp of a date is greater, more time has elapsed since the Unix Epoch.

If the timestamps of the two dates are equal, then the year, month, day, hour, minutes, seconds and milliseconds values for the two dates are equal.

If you have difficulties creating a valid Date object from a date string, you can pass 2 types of parameters to the Date() constructor:

  1. a valid ISO 8601 string, formatted as YYYY-MM-DDTHH:mm:ss.sssZ, or just YYYY-MM-DD, if you only have a date without time.
  2. multiple, comma-separated parameters that represent the year, month (0 = January to 11 = December), day of the month, hours, minutes and seconds.

Here is an example that splits a string and passes the parameters to the Date() constructor to create a Date object.

// 👇️ Formatted as MM/DD/YYYY const dateStr = '06/21/2022'; const [month, day, year] = dateStr.split('/'); // 👇️ Create valid Date object const date = new Date(+year, month - 1, +day); console.log(date); // 👉️ Tue Jun 21 2022

The date string is formatted as mm/dd/yyyy, but the approach applies to any other format.

Notice that we subtracted 1 from the month when passing it to the Date() constructor.

This is because, the Date constructor expects a zero-based value, where January = 0, February = 1, March = 2, etc.

We split the string on each forward slash to get an array of substrings.

const str = '06/21/2022'; // 👇️ ['06', '21', '2022'] console.log(str.split('/'))

We used array destructuring to assign the month, day and year values to variables and passed them to the Date() constructor.

Once you have created Date objects from the date strings, you can check if a date is after another date by comparing them as you would compare numbers in JavaScript.
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.