Check if Date is Before or After another Date in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
8 min

banner

# Table of Contents

  1. Check if a Date is before another Date using JavaScript
  2. Check if a Date is after another Date using JavaScript
  3. Check if a Date is in the Past using JavaScript
  4. Check if a Date is in the Future using JavaScript

# Check if a Date is before another Date using JavaScript

To check if a date is before another date, compare the Date objects, e.g. date1 < date2.

If the comparison returns true, then the first date is before the second, otherwise, the first date is equal to or comes after the second.

index.js
function isBefore(date1, date2) { return date1 < date2; } const d1 = new Date('2022-02-24'); const d2 = new Date('2022-09-11'); console.log(isBefore(d1, d2)); // ๐Ÿ‘‰๏ธ true console.log(isBefore(d2, d1)); // ๐Ÿ‘‰๏ธ false

check if date is before another date

The code for this article is available on GitHub

We created a reusable function that takes 2 Date objects as parameters and checks if the first date is before 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.

index.js
const date = new Date('2022-09-24'); // ๐Ÿ‘‡๏ธ 1663977600000 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 greater 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.

index.js
// ๐Ÿ‘‡๏ธ Formatted as MM/DD/YYYY const dateStr = '08/24/2022'; const [month, day, year] = dateStr.split('/'); // ๐Ÿ‘‡๏ธ Create valid Date object const date = new Date(+year, month - 1, +day); console.log(date); // ๐Ÿ‘‰๏ธ Wed Aug 24 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 String.split the string on each forward slash to get an array of substrings.

index.js
const str = '08/24/2022'; // ๐Ÿ‘‡๏ธ ['08', '24', '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 before another date by comparing them as you would compare numbers in JavaScript.

# 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.

index.js
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

check if date is after another date

The code for this article is available on GitHub

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.

index.js
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.

index.js
// ๐Ÿ‘‡๏ธ 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 String.split the string on each forward slash to get an array of substrings.

index.js
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.

# Check if a Date is in the Past using JavaScript

To check if a date is in the past:

  1. Use the Date() constructor to get the current date.
  2. Optionally set the time of the current date to midnight.
  3. Check if the date is less than the current date.
index.js
function isInThePast(date) { const today = new Date(); // ๐Ÿ‘‡๏ธ OPTIONAL! // This line sets the hour of the current date to midnight // so the comparison only returns `true` if the passed-in date // is at least yesterday today.setHours(0, 0, 0, 0); return date < today; } console.log(isInThePast(new Date())); // ๐Ÿ‘‰๏ธ false console.log(isInThePast(new Date('2022-01-25'))); // ๐Ÿ‘‰๏ธ true

check if date is in the past

The code for this article is available on GitHub

If you need to check if a Date is in the Future, scroll down to the next subheading.

We created a reusable function that takes a Date object as a parameter and checks if the date is in the past.

We used the Date() constructor to create a Date object that represents the current date.

The next line uses the setHours() method to set the hour, minutes, seconds and milliseconds of the date to 0 (midnight).

This line is optional and enables us to check if the passed in Date is at least yesterday.

If you remove this line, you will be checking if the supplied date is at least a millisecond in the past and not at least a day in the past.

Here is the same example without setting the hour, minutes, seconds and milliseconds to 0.

index.js
function isInThePast(date) { const today = new Date(); return date < today; } const oneHourAgo = new Date(); oneHourAgo.setHours(oneHourAgo.getHours() - 1); console.log(isInThePast(oneHourAgo)); // ๐Ÿ‘‰๏ธ true console.log(isInThePast(new Date('2050-03-24'))); // ๐Ÿ‘‰๏ธ false
Now the function checks if the provided date is at least 1 millisecond in the past, instead of at least 1 day.

We can 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.

index.js
const date = new Date('2022-08-23'); // ๐Ÿ‘‡๏ธ 1661212800000 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.

# Check if a Date is in the Future using JavaScript

To check if a date is in the future:

  1. Use the Date() constructor to get the current date.
  2. Optionally set the time of the current date to the last millisecond.
  3. Check if the date is greater than the current date.
index.js
function isInTheFuture(date) { const today = new Date(); // ๐Ÿ‘‡๏ธ OPTIONAL! // This line sets the time of the current date to the // last millisecond, so the comparison returns `true` only if // date is at least tomorrow today.setHours(23, 59, 59, 998); return date > today; } const tomorrow = new Date(); tomorrow.setDate(tomorrow.getDate() + 1); console.log(isInTheFuture(tomorrow)); // ๐Ÿ‘‰๏ธ true console.log(isInTheFuture(new Date('2021-01-25'))); // ๐Ÿ‘‰๏ธ false

check if date is in the future

The code for this article is available on GitHub

We created a reusable function that takes a DAte object as a parameter and checks if the date is in the future.

The first thing we did was use the Date() constructor to create a Date object that represents the current date.

The next line uses the setHours method to set the hour, minutes, seconds and milliseconds of the date to the last millisecond of the current date.

This line is optional and enables us to check if the passed in Date is at least tomorrow's date.

If you remove this line, you will be checking if the passed-in date is at least a millisecond in the future and not at least a day in the future.

Here is the same example without setting the hour, minutes, seconds and milliseconds to the last millisecond of the current date.

index.js
function isInTheFuture(date) { const today = new Date(); return date > today; } const oneHourInTheFuture = new Date(); oneHourInTheFuture.setHours(oneHourInTheFuture.getHours() + 1); console.log(isInTheFuture(oneHourInTheFuture)); // ๐Ÿ‘‰๏ธ true console.log(isInTheFuture(new Date('2021-01-25'))); // ๐Ÿ‘‰๏ธ false
Now the function checks if the passed-in date is at least 1 millisecond in the future, instead of at least 1 day in the future.

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.

index.js
const date = new Date('2022-10-26'); // ๐Ÿ‘‡๏ธ 1666742400000 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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev