In this tutorial, I will show you how to validate a date formatted as DD/MM/YYYY in JavaScript. There’re many ways to get it done, but we will demonstrate one common one. Please read the entire article to understand what I want to convey.
Why do you need to validate a date formatted as DD/MM/YYYY?
Validate a Date formatted as DD/MM/YYYY in JavaScript may help you easily configure date, month, and year, to check the user has input the correct format of Date. Below is one of the most popular developers often use.
Validate a date formatted as DD/MM/YYYY in JavaScript
Let’s follow these steps to validate a date formatted as DD/MM/YYYY in JS:
Step 1: Create a function to check if input data is correctly formatted or not.
For example:
function inputDateIsValid(inputDate) {
const formatOfDate = /^\d{2}\/\d{2}\/\d{4}$/;
const [getDay, getMonth, getYear] = inputDate.split("/");
if (inputDate.match(formatOfDate) === null) {
return false;
} else {
return true;
}
}
Now, I input a date as 27/01/2022 by:
console.log(inputDateIsValid("27/08/2022"));
Console Output
true
If I input the error date as 132/01/2022:
console.log(inputDateIsValid("132/02/2022"));
Console Output
false
In this program, the function gives a parameter in the structure “dd/mm/yyyy”, and the formatOfDate variable has to check it.
Step 2: Take a string, formatting it as “yyyy/mm/dd’’ and pass them to a Date() constructor. See if it got a legitimate ‘Date’ object returned.
For example:
function inputDateIsValid(inputDate) {
const formatOfDate = /^\d{2}\/\d{2}\/\d{4}$/;
const [getDay, getMonth, getYear] = inputDate.split("/");
if (inputDate.match(formatOfDate) === null) {
return false;
}
// `YYYY/MM/DD` format
const isoFormattedDate = `${getYear}-${getMonth}-${getDay}`;
const dating = new Date(isoFormattedDate);
const timeStamping = dating.getTime();
if (typeof timeStamping !== "number" || Number.isNaN(timeStamping)) {
return false;
}
return dating.toISOString().startsWith(isoFormattedDate);
}
alert(inputDateIsValid("27/06/2015"));
alert(inputDateIsValid("32/02/2015"));
alert(inputDateIsValid("World"));
Alert:
true
false
false
The ‘dating’ variable has stored ‘Invalid Date’. Now, we use ‘getTime’ method to calculate the milliseconds from the 1st January 1970 to the present. Next, we use ‘if’ condition to check ‘timestamping’. If that is not stored a number, it will return ‘NaN’ (not a number). Finally, we use toISOString() method to return a format as ‘YYYY/MM/DDTHH:mm:ss.zzzZ’ and convert it to string.
Summary
These are all tutorials on how to validate a date formatted like DD/MM/YYYY in JavaScript. After reading the post, I hope you will learn how to fix this error. I hope this tutorial is helpful to you. Happy coding.
Maybe you are interested:
- Increment a Date by 1 Month using JavaScript
- Get the GMT Hours using JavaScript
- Get the current Date and Time in Seconds using JavaScript

Hello, my name is Philip Cabrera. My job is a programmer, and I have 4 years of programming expertise. Follow me if you need assistance with C, C++, JS, TS, or Node.js. I hope my posts are useful to you.
Name of the university: HUMG
Major: IT
Programming Languages: C, C++, JS, TS, Node.js