Categories
JavaScript Answers

How to validate if date is before date of current date with JavaScript?

Spread the love

Sometimes, we want to validate if date is before date of current date with JavaScript.

In this article, we’ll look at how to validate if date is before date of current date with JavaScript.

How to validate if date is before date of current date with JavaScript?

To validate if date is before date of current date with JavaScript, we compare their timestamps.

For instance, we write

const isAfterToday = (date) => {
  return new Date(date).valueOf() > new Date().valueOf();
};

to define the isAfterToday function.

In it, we compare the date date with today’s date by converting to timestamps with the valueOf method.

Then we compare if date is bigger than today’s date with >.

Conclusion

To validate if date is before date of current date with JavaScript, we compare their 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 *