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.