We will learn how to validate a date formatted as DD/MM/YYYY in JavaScript with two methods: Using split()
, reverse()
and join()
and using moment.js. This basic knowledge is very useful when you work with dates in Javascript.
Validate a date formatted as DD/MM/YYYY in JavaScript
Using split(), reverse() and join()
The first function works on a string object, the remaining two functions operate on an array.
Syntax:
split(separator)
reverse()
join(separator)
Parameters:
- separator: The slash (
/
) character to separate each part of the date, such as date, month and year.
You can use those functions to convert a string into an array of three elements of date (day, month, and year respectively), then reverse the array’s elements and join it into a string of the correct format of a date.
let string = '30/10/2022'; let revString = string.split('/').reverse().join('/'); console.log(revString); let dateObj = new Date(revString); console.log(dateObj.toLocaleDateString());
Output:
2022/10/30
10/30/2022
The above example points out that the original string given is in DD/MM/YYYY format. However this format cannot be used to create a date object as the Date()
constructor only accepts the date formatted as YYYY/MM/DD, which is the reverse pattern of the original format. So our idea is to split the original string into an array that contains the values [‘DD’, ‘MM’, ‘YYYY’]
and then reverse the array, resulting in [‘YYYY’, ‘MM’, ‘DD’]
. Finally, merge it into a string with a slash separator to result in a string formatted as YYYY/MM/DD. However, if the date value you provided is not valid, the Date()
object cannot be created, and the result would be like this:
let string = '99/10/2022'; let revString = string.split('/').reverse().join('/'); console.log(revString); let dateObj = new Date(revString); console.log(dateObj.toLocaleDateString());
Output:
2022/10/99
Invalid Date
As a result, you can check its validity by comparing it to the string “Invalid Date”, for example:
let string = '99/10/2022'; let revString = string.split('/').reverse().join('/'); console.log(revString); let dateObj = new Date(revString); let dateString = dateObj.toLocaleDateString(); if (dateString == 'Invalid Date') { console.log('The Date is not valid, please try another'); }
Output:
2022/10/99
The Date is not valid, please try another
Using moment.js
With this method, you have to import the library at the script tag of your HTML document.
<script src="https://momentjs.com/downloads/moment.js"></script>
Now you may easily use this library to convert a string to a date and validate formatted as DD/MM/YYYY by using the moment()
method:
Syntax:
moment(string, format)
Parameters:
- string: This parameter is a string that represents a date format as DD/MM/YYYY
- format: The string represents the format of the outdated string. In our case, it is DD/MM/YYYY
This function accepts a date string and a string that defines the format and returns the date.
let string = "95/10/2022"; let stringToMoment = moment(string, "DD/MM/YYYY"); console.log(stringToMoment.isValid());
Output:
false
The example above uses the isValid()
method of the moment
object to check whether the date provided is a valid date or not. If it is a valid date, you can print it like any Date object.
let string = "5/10/2022"; let stringToMoment = moment(string, "DD/MM/YYYY"); console.log(stringToMoment.isValid()); console.log(stringToMoment.format())
Output:
true
2022-10-05T00:00:00+07:00
Summary
We have learned how to validate a date formatted as DD/MM/YYYY in JavaScript. It would be necessary to remember that each approach has its pros and cons. You can validate the date and format by using our methods in this tutorial and other ones. Thanks for reading!
Maybe you are interested:
- Get the Start and End of the Day using JavaScript
- Get ISO Date without the Milliseconds using JavaScript
- Get the Day Name from a specific Date using JavaScript

I’m Edward Anderson. My current job is as a programmer. I’m majoring in information technology and 5 years of programming expertise. Python, C, C++, Javascript, Java, HTML, CSS, and R are my strong suits. Let me know if you have any questions about these programming languages.
Name of the university: HCMUT
Major: CS
Programming Languages: Python, C, C++, Javascript, Java, HTML, CSS, R