How to Check if a Date is Before Today in JavaScript?

To check if a date is before today, first get the today’s date object by calling the Date() constructor and then compare it with the specified date object eg. date < today. If the comparison of the two date objects returns true, it means the given date is before today, otherwise not.

For example,

// Today's date(Mar 20, 2023)
const today = new Date();

// Mar 15, 2023
const date = new Date('2023-03-15');

// Set hours, minutes, seconds and milliseconds to 0
today.setHours(0, 0, 0, 0);
date.setHours(0, 0, 0, 0);

// Compare the date objects
if(date < today){
    console.log('The date is before today.');
}
else if(date > today){
    console.log('The date is after today.')
}
else {
    console.log("The date is today.")
}

Output:

The date is before today.

Now you might be wondering how does the comparison work with dates?

Actually, when you compare two date objects, behind the scenes, each date object is first converted to its equivalent timestamp( total number of seconds elapsed since January 1st, 1970) and then the comparison is performed on the timestamp values, not on the date objects.

Since the timestamp is just a number, therefore, you can use the comparison operators to compare two date objects.

You can also manually convert the given dates to timestamps and then compare their timestamps to check if the given date is before today or not.

To convert a date to timestamp, you call call the getTime() method on the date object. The getTime() method returns an integer denoting the timestamp for the specified date.

For example,

// Today's date(Mar 20, 2023)
const today = new Date();

// Mar 15, 2023
const date = new Date('2023-03-15');

// Set hours, minutes, seconds and milliseconds to 0
today.setHours(0, 0, 0, 0);
date.setHours(0, 0, 0, 0);


// Get timestamps
todayTimestamp = today.getTime();  // 1679250600000
dateTimestamp = date.getTime();  // 1678818600000


// Compare the timestamps
if(dateTimestamp < todayTimestamp){
    console.log('The date is before today.');
}
else if(dateTimestamp > todayTimestamp){
    console.log('The date is after today.')
}
else {
    console.log("The date is today.")
}

Output:

The date is before today.

If you have noticed, we used the setHours(0, 0, 0, 0) method to set the hours, minutes, seconds and milliseconds to 0 for both dates before comparison. But why?

That’s because when we create a date object using the Date() constructor, it gives us the date object along with the time.

For example,

// Get current date time
const today = new Date();

console.log(today);
// Output: 👉 Mon Mar 20 2023 11:06:18 GMT+0530 (India Standard Time)

But when comparing the dates to check if the date is before today or not, we only care about the date part, not the time.

For example, if today is March 20, 2023, we want to consider any date before March 20, 2023 as “before today”, regardless of the time of day.

That’s the reason we use the setHours(0, 0, 0, 0) method to set the time for both the dates to 0.

For example,

// Today's date
const today = new Date();

console.log(today);
// Output: 👉 Mon Mar 20 2023 11:12:25 GMT+0530 (India Standard Time)

// Set time to 0
today.setHours(0, 0, 0, 0);

console.log(today);
// Output: 👉 Mon Mar 20 2023 00:00:00 GMT+0530 (India Standard Time)

Use the toISOString() Method to Check if a Date is Before Today

The toISOString() method returns a string representation of the date in ISO format when called on a date object.

We can directly use the ISO strings to compare if a date is before today or not.

For example,

// Today's date(Mar 20, 2023)
const today = new Date();

// Mar 15, 2023
const date = new Date('2023-03-15');

// Get the date as "yyyy-mm-dd" string
todayStr = today.toISOString().slice(0, 10); // "2023-03-20"
dateStr = date.toISOString().slice(0, 10); // "2023-03-15"

// Compare the date strings
if(dateStr < todayStr){
    console.log('The date is before today.');
}
else if(dateStr > todayStr){
    console.log('The date is after today.')
}
else {
    console.log("The date is today.")
}

Output:

The date is before today.

When you call the toISOString() method on a date object, it returns the date string along with the time.

Therefore, we call the slice() method on the date string to get only the date part, not the time.

For example,

// Today's date
const today = new Date();

// Get the date in string format
const dateStr = today.toISOString();

console.log(dateStr);
// Output: 👉 "2023-03-20T06:11:22.388Z"

// Get the first 10 characters of the date string
const slicedStr = dateStr.slice(0, 10);

console.log(slicedStr);
// Output: 👉 "2023-03-20"

You can use any of the three methods to check if a date is before today. Each method gives the same result.

The first two methods use the timestamp to compare the dates whereas the third method gets the string version of the dates and then compares the date strings directly.

That’s all for this article. Thanks for reading!

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.