Borislav Hadzhiev
Last updated: Jul 25, 2022
Photo from Unsplash
To check if a date is between two dates:
Date()
constructor to convert the dates to Date
objects.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.
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.
const date = new Date('2022-09-24'); // 👇️ 1663977600000 console.log(date.getTime());
getTime()
method on each date.We used the
logical AND (&&)
operator, which checks if both conditions are met before running our if
block.
If you have difficulties creating a valid Date
object from your date
strings, you can pass 2 types of parameters to the Date()
constructor:
YYYY-MM-DDTHH:mm:ss.sssZ
, or just
YYYY-MM-DD
, if you only have a date without time.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 = '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.
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 = '07/21/2022'; // 👇️ ['07', '21', '2022'] console.log(str.split('/'))
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.
// 👇️ 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.
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.