How to check if a Date is in the Past or Future in JavaScript

In this article, you’ll learn how to check if a Date is in the Past or Future in JavaScript by comparing the specified date to the current date.

Checking if a Date is in the Past or Future

Solution

As a reminder, the Date class represents ECMAScript‘s epoch i.e. the number of milliseconds that have passed since  January 1st 1970 (which specifically is 1970-01-01 00:00:00:00 UTC+00) in your computer’s set timezone.

With this definition, you simply need to check whether the specified Date is larger or smaller than the current Date, which can be accessed with the Date.now() or an empty Date constructor (new Date()) function. As such:

Code: (As of 2022-11-03)

const future = new Date(2023,0,21); // January 21st 2023
const past = new Date(2022,8); // August 2022 - LearnShareIT Twitter creation Date

function isInFuture(date) {
    const now = Date.now(); // 2022-11-03 as of the writing of the code
    return date > now; // If the date's value is greater than the current date. 
}

function isInPast(date) {
    const now = Date.now(); // 2022-11-03 as of the writing of the code
    return date < now; // If the date's value is smaller than the current date. 
}

console.log(isInFuture(future));
console.log(isInFuture(past));
console.log(isInPast(future));
console.log(isInPast(past));

Output:

true
false
false
true

Sidenotes

If you want to check whether a Date is today, you cannot do the same as above; as a Date is the number of milliseconds since January 1st 1970 which includes seconds. So comparing the same Date at 2am is still considered in the past if it’s 5am.

To remedy this you need to set both dates’ time to 00:00:00:000 (or any equal time). As such:

Code: (As of 2022-11-03)

const today = new Date(2022,10,03); // 2022-11-03 00:00:00
const notToday = new Date(2012,1,30); // 2012-02-30 00:00:00

function isInPresent(date) {
    const now = new Date(); // 2022-11-03 HH:MM:SS as of the writing of the code
    const midnightDate = setDateToMidnight(date);
    const midnightNow = setDateToMidnight(now);
    return midnightNow.valueOf() === midnightDate.valueOf() ; // If the date's value is equal to the current date. 
}

function setDateToMidnight(date) { // Returns a Date at 00:00:00:000
    const midnightDate = new Date(date.valueOf()); // Get the date's epoch as the constructor value
    midnightDate.setHours(0);
    midnightDate.setMinutes(0);
    midnightDate.setSeconds(0);
    midnightDate.setMilliseconds(0);
    return midnightDate;
}

console.log(isInPresent(today));
console.log(isInPresent(notToday));

Output:

true
false

A thing to note is that instead of getting the current date using the Date.now() function which returns a number, it would be preferable to use an empty Date constructor instead (which returns an instance of the Date class) as setting the time to midnight would be much easier with the Date class’ provided functions.

The Date’s constructor or Date() has multiple ways of creating a Date from a value like using an epoch as seen in line 12 (the .valueOf() function returns an epoch), taking date values like in line 1 or even nothing at all which will return the current date at midnight (as seen in line 5).

Additionally, note the usage of the .valueOf() function which returns the epoch of the Date in line 8. In the previous code example, the date (instance of the Date class) is compared to Date.now() which as stated above is a number; this does not work if you use regular or string equality (== or ===) however. That is because when you are checking equality in terms of classes (i.e. the Date class), you are checking whether they are the same or referring to the same instance of that class; so to deal with this it is required that instead you check if the value (i.e. the epoch) are equal.

Summary

To check if a Date is in the Past or Future, you need to compare the specified Date with the current date and time which can be accessed with the Date.now() function or an empty Date() constructor.

Leave a Reply

Your email address will not be published. Required fields are marked *