Check If A Date Is Before Today’s Date In JavaScript

Check if a Date is before Today's date in JavaScript

This article will show you how to check if a date is before Today’s date in JavaScript by using two different methods: using new Date() with setHours() and using getDate(), getMonth() with getYear().

How to check if a date is before Today’s date in JavaScript

Using new Date() and set Hours()

You can read the syntax of those methods here. Assume you have a date object which represents 3 pm of 12/11/2022; you can compare it to the Date object which represents the beginning of today, for example:

// Create a Date object of today's Date (midnight)
let today = new Date()
today.setHours(0,0,0,0)

// Create a Date object of 12/11/22 at 15:00:00
let myDate = new Date(2022,10,12,15,0,0,0)

// Check if myDate is before today
console.log(myDate<today)

Output:

true

The logic behind this method is very simple. As the new Date() constructor will return a long integer that represents its UNIX timestamp. By comparing the timestamp of the Date object which you want to check with the timestamp of the Date object of today’s begin, if it is smaller than that value, then you can assume that a date is before Today’s date.

Using getDate() and getMonth() and getYear()

Those three methods getDate(), getMonth() and getYear() are used to get the day, the month index and the year of the date which the Date object is representing. These are the most popular methods when working with Date. We have introduced their syntax and usage here, so we will go straight forward to the point. Example:

// Create a Date object of today (midnight)
let today = new Date()

// Create a Date object of 12/11/22 at 15:00:00
let myDate = new Date(2022,10,12,15,0,0,0)

// If they are smaller in year, return true
if (myDate.getYear() < today.getYear())
    console.log(true);

// If they are larger in year, return false
else if (myDate.getYear() > today.getYear())
    console.log(false);

// Year is equal, if they are smaller in monthIndex, return true
else if (myDate.getMonth() < today.getMonth())
    console.log(true);

// Year is equal, if they are larger in monthIndex, return false
else if (myDate.getMonth() > today.getMonth())
    console.log(false);

// Year and monthIndex are equal, if they are smaller in Day, return true
else if (myDate.getDate() < today.getDate())
    console.log(true);

// Otherwise it is the same date or the future day, return false
else console.log(false);

Output:

true

As you can see, this method will produce the same result as the first one. Although this approach will consume many if-else statements, it is much simpler to understand the logic for JavaScript beginners. However, if you are experienced with this language, you can use the first approach instead because it is optimized and efficient.

Summary

This article has shown how to check if a date is before Today’s date in JavaScript. We hope the information in this tutorial will be helpful to you. If you have any questions, feel free to leave a reply, as we will answer soon. You should use the first method if you have gotten acquainted with Date.

Maybe you are interested:

Leave a Reply

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