Sometimes, we want to validate date with format mm/dd/yyyy in JavaScript.
In this article, we’ll look at how to validate date with format mm/dd/yyyy in JavaScript.
How to validate date with format mm/dd/yyyy in JavaScript?
To validate date with format mm/dd/yyyy in JavaScript, we can use moment.js.
For instance, we write
console.log(moment("05/22/2022", "MM/DD/YYYY", true).isValid());
to check that "05/22/2022"
matches the "MM/DD/YYYY"
format with moment
.
We call moment
with true
to make sure the date string matches the date format in the 2nd argument exactly.
Then we call isValid
to return whether the date string is valid in the given format or not.
Conclusion
To validate date with format mm/dd/yyyy in JavaScript, we can use moment.js.