Categories
JavaScript Answers

How to check that a date is greater than 30 days from today’s date with JavaScript?

Spread the love

Sometimes, we want to check that a date is greater than 30 days from today’s date with JavaScript.

In this article, we’ll look at how to check that a date is greater than 30 days from today’s date with JavaScript.

How to check that a date is greater than 30 days from today’s date with JavaScript?

To check that a date is greater than 30 days from today’s date with JavaScript, we can compare the timestamps of both dates.

For instance, we write:

const today = new Date()
const d = new Date(2022, 10, 10, 10)
const greaterThan30Days = +d > +today + 30 * 24 * 60 * 60 * 1000
console.log(greaterThan30Days)

to create 2 dates, today and d.

And then we check if d is more than 30 days after today by converting them both to timestamps in milliseconds with +

Then we add 30 days in milliseconds with 30 * 24 * 60 * 60 * 1000 to today.

And then we use > to do the comparison.

Therefore, greaterThan30Days is true.

Conclusion

To check that a date is greater than 30 days from today’s date with JavaScript, we can compare the timestamps of both dates.

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 *