Categories
JavaScript Answers

How to check if input date is equal to today’s date with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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