Last updated: Mar 6, 2024
Reading timeยท8 min
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.
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
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.
const date = new Date('2022-09-24'); // ๐๏ธ 1663977600000 console.log(date.getTime());
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 you have difficulties creating a valid Date
object from a date string,
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 = '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.
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.
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.
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());
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 you have difficulties creating a valid Date
object from a date string,
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 = '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.
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.
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.
To check if a date is in the past:
Date()
constructor to get the current date.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
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.
Here is the same example without setting the hour, minutes, seconds and
milliseconds to 0
.
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
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.
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.
To check if a date is in the future:
Date()
constructor to get the current date.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
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.
Here is the same example without setting the hour, minutes, seconds and milliseconds to the last millisecond of the current date.
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
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-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.
You can learn more about the related topics by checking out the following tutorials: