Sometimes, we want to check if input date is equal to today’s date with JavaScript.
In this article, we’ll look at how to check if input date is equal to today’s date with JavaScript.
How to check if input date is equal to today’s date with JavaScript?
To check if input date is equal to today’s date with JavaScript, we can com[are the dates’ timestamps.
For instance, we write
const inputDate = new Date("11/21/2022");
const todaysDate = new Date();
if (inputDate.setHours(0, 0, 0, 0) === todaysDate.setHours(0, 0, 0, 0)) {
// ...
}
to compare the timestamps of inputDate
and todaysDate
after we call setHours
on each to set their times to midnight.
When we use ===
to compare them, their timestamps are compared.
So if their timestamps are the same, then they’re the same date.
Conclusion
To check if input date is equal to today’s date with JavaScript, we can com[are the dates’ timestamps.