Check if a Date is between Two Dates using JavaScript

avatar

Borislav Hadzhiev

Last updated: Jul 25, 2022

banner

Photo from Unsplash

Check if a Date is between Two Dates using JavaScript #

To check if a date is between two dates:

  1. Use the Date() constructor to convert the dates to Date objects.
  2. Check if the date is greater than the start date and less than the end date.
  3. If both conditions are met, the date is between the two dates.
index.js
const date = new Date('2022-09-24'); const start = new Date('2022-03-18'); const end = new Date('2022-11-22'); if (date > start && date < end) { console.log('✅ date is between the 2 dates'); } else { console.log('⛔️ date is not in the range'); }

We passed the date strings to the Date() constructor to create Date objects.

If passing the date string to the Date() constructor doesn't return a valid date, then you have to format your date strings differently, e.g. yyyy-mm-dd (more on that below).

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.

We used the logical AND (&&) operator, which checks if both conditions are met before running our if block.

If the date is greater than the start date and less than the end date, then it is between the two dates.

If you have difficulties creating a valid Date object from your date strings, 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 = '07/21/2022'; const [month, day, year] = dateStr.split('/'); // 👇️ Create valid Date object const date = new Date(+year, month - 1, +day); console.log(date); // 👉️ Thu Jul 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.

index.js
const str = '07/21/2022'; // 👇️ ['07', '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 just compare the dates as you would compare numbers because Date objects get converted to a timestamp before the comparison takes place.

index.js
// 👇️ Formatted as MM/DD/YYYY const dateStr = '07/21/2022'; const [month1, day1, year1] = dateStr.split('/'); const date = new Date(+year1, month1 - 1, +day1); const startStr = '03/24/2022'; const [month2, day2, year2] = startStr.split('/'); const startDate = new Date(+year2, month2 - 1, +day2); const endStr = '11/28/2022'; const [month3, day3, year3] = endStr.split('/'); const endDate = new Date(+year3, month3 - 1, +day3); if (date > startDate && date < endDate) { console.log('✅ date is between start and end dates'); } else { console.log('⛔️ date is NOT between start and end dates'); }

We repeated the process for each of the dates and split each on the forward slashes to get the month, day and year values.

It's very important to subtract 1 from the value of the month, because the Date() constructor expects a zero-based value for the month (January = 0, February = 1, March = 2).

The if block from the example runs because the date is between the start and end dates.

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.