We have many functions in Javascript that help us work with Date objects. With the question: how to validate a date formatted as MM/DD/YYYY in JavaScript? It is not difficult. We are going to show some common methods to do it like using Date.parse()
method or using regular expression. Let’s check it out!
How To Validate A Date Formatted As MM/DD/YYYY In JavaScript
Use Date.parse() method
Date.parse()
will parse the given string to see if it is a date string. If valid, returns the time difference starting from January 1, 1970. This period is in milliseconds. If false returns a NaN
value.
Syntax:
Date.parse(value)
Parameters:
- value: This is the date format string you want to check.
Example:
function isValidDate(value) { let result = Date.parse(value); if (!isNaN(result)) { return true; } else { return false; } } console.log(isValidDate("12/24/2021")); console.log(isValidDate("13/24/2021"));
Output:
true
false
In this example, when we use Date.parse()
to process, the program can still understand the Date formatted as MM/DD/YYYY, but this is not the most optimal and reasonable method in some cases. However, using the Date.parse()
method to check if the string is a date format is still a good idea as it will be faster.
Use regular expression
We can use a regular expression to check the Date formatted as MM/DD/YYYY by checking the passed date format string with a regular expression. If true, passed to check whether the Date is valid or not, then return true/false results.
Syntax:
/^\d{2}\/\d{2}\/\d{4}$/
Using this regular expression, we can check if the input string is correct MM/DD/YYYY format where M, D and Y are numbers. With this method, I would check the formatted Date string as MM/DD/YYYY as follows:
function isValidDate(value) { const regex = /^\d{2}\/\d{2}\/\d{4}$/; if (!regex.test(value)) { return false; } const temp = value.split('/').map((p) => parseInt(p, 10)); const date = new Date(temp[2], temp[0] - 1, temp[1]); return date.getMonth() === temp[0] - 1 && date.getDate() === temp[1] && date.getFullYear() === temp[2]; } console.log(isValidDate("12/24/2021")); console.log(isValidDate("13/24/2021"));
Output:
true
false
I use a regular expression to check the input string in the above example. After the string is valid, I will use the split()
and map()
method to split the string and convert the substrings to numbers. Then, I create a new date
from those. The new value is obtained from the split()
method, finally using the value of the newly created date
to compare with the numbers we have retrieved to see if they match. If not, it means that the number we passed in is not correct compared to the date format MM/DD/YYYY.
Summary
In the above article, you can learn about how to validate a date formatted as MM/DD/YYYY in JavaScript. Please follow the article carefully. I think it will help you and your program. Good luck.
Maybe you are interested:
- Get Month and Date in 2 Digit Format in JavaScript
- Format a Date as MM/DD/YYYY hh:mm:ss in JavaScript
- Format a Date as YYYY-MM-DD hh:mm:ss in JavaScript

My name is Tom Joseph, and I work as a software engineer. I enjoy programming and passing on my experience. C, C++, JAVA, and Python are my strong programming languages that I can share with everyone. In addition, I have also developed projects using Javascript, html, css.
Job: Developer
Name of the university: UTC
Programming Languages: C, C++, Javascript, JAVA, python, html, css