In this article, we are going to learn the validation of string date format in JavaScript. Validating a String date format involves checking if the given date follows a specified format, ensuring it conforms to the expected arrangement of day, month, and year components.
There are many ways by which we can validate whether the Date format is correct or not, Let’s discuss each of them one by one.
Table of Content
We will explore all the above methods along with their basic implementation with the help of examples.
Approach 1: Using the Date.parse() Inbuild Function
In this approach, we are using Date.parse() that takes a string as an argument to calculate the milliseconds since “1-jan-1970” and returns the result in milliseconds. NaN will check for the number validation if it returns false means it is not a valid date else it is a valid date.
Syntax:
Date.parse( datestring );
Example: It describes validation of date format using Date.parse() inbuild function.
Javascript
// Creating variables let Date1 = "2023-08-25" ; let Date2 = "Invalid date string format" ; // Function for validation of date format function isValidDate(stringDate) { return !isNaN(Date.parse(stringDate)); } // Displaying the result console.log(isValidDate(Date1)); console.log(isValidDate(Date2)); |
true false
Approach 2: Using Regular Expression
For date format validation, we are using regular expressions. This entails creating a pattern to match particular text formats, which can be customized according to the required date format.
Type of formats:
- “YYYY-MM-DD”
- “DD-MM-YYYY”
- “DD/MM/YYYY”
Syntax:
// Format 1: "YYYY-MM-DD" const regex = /^\d{4}-\d{2}-\d{2}$/; // Format2 : "DD-MM-YYYY" const regex = /^(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])-(\d{4})$/;
// Format3 : "DD/MM/YYYY" const regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1[0-2])\/\d{4}$/;
Example: It describes the string date validation using regular expression in different ways.
Javascript
let Date1 = "06-24-2013" ; let Date2 = "2013-18-09" ; // Format 1: "YYYY-MM-DD" (Y-year, M- month, D - date) function isValidDate(stringDate) { const regex = /^\d{4}-\d{2}-\d{2}$/; return regex.test(stringDate); } console.log(isValidDate(Date1)); // Output: false console.log(isValidDate(Date2)); // Output: true // Format2 : "DD-MM-YYYY" (Y-year , M- month , D - date) let Date3 = "2013-18-09" ; let Date4 = "06-24-2013" ; function isValidDate1(stringDate) { const regex = /^(0[1-9]|1[0-2])-(0[1-9]|[12][0-9]|3[01])-(\d{4})$/; return regex.test(stringDate); } console.log(isValidDate1(Date3)); // Output: false console.log(isValidDate1(Date4)); // Output: true // Format3 : "DD/MM/YYYY" (Y-year , M- month , D - date) let Date5 = "2013-18-09" ; let Date6 = "06-24-2013" ; let Date7 = "06/12/2013" ; let Date8 = "06/4/2013" ; function isValidDate2(stringDate) { const regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1[0-2])\/\d{4}$/; return regex.test(stringDate); } console.log(isValidDate2(Date5)); // Output: false // Month can't be more than 12 console.log(isValidDate2(Date6)); // Output: false console.log(isValidDate2(Date7)); // Output: true // Month should have 2 digits console.log(isValidDate2(Date8)); // Output: false |
false true false true false false true false
Approach 3: Using Instanceof Operator
In this approach we are using instanceof operator which makes instance of date and if it is a valid date object then it will return true else it will return false. The !isNan() function can be used to determine whether the date in the Date object is valid.
Syntax:
let gfg = objectName instanceof objectType
Example: It describes the string date validation using instance of operator.
Javascript
// Creating variable having different values let str = "13/2019/jan" ; let str1 = "2019/jan/13" ; let str2 = "jan/13/2019" ; let str3 = "13/13/2019" ; // Function for validation of date function isValidDate(str) { let date = new Date(str); let ans = (date instanceof Date) && !isNaN(date); return ans; } // Printing out the result in console console.log(isValidDate(str)); //true console.log(isValidDate(str1)); //true console.log(isValidDate(str2)); //true //false : invalid daate format console.log(isValidDate(str3)); |
true true true false
Approach 4: Using Object.prototype.toString.call() Method
Regardless of the date format, the JavaScript code Object.prototype.toString.call(date) === ‘[object Date]’ is frequently used to verify whether an object is an instance of the Date class. It return the date object if it is a valid date object and it is not NaN then it will true else it will return false.
Syntax:
obj.toString()
Example: It describes the string date validation using Object.prototype.toString.call() function.
Javascript
// Function for validation of date format function isValiDate(str) { let date = new Date(str); let ans = Object.prototype.toString.call(date) === "[object Date]" && !isNaN(date); return ans; } // DD-MM-YYYY let str = "13-jan-2019" ; // MM-DD-YY let str1 = "jan-19-13" ; // DD-YY-MM let str2 = "13-19-jan" ; // DD/YY/MM let str3 = "13/19/jan" ; // DD/YY/MM (month in number) let str4 = "13/19/01" ; console.log(isValiDate(str)); console.log(isValiDate(str1)); console.log(isValiDate(str2)); console.log(isValiDate(str3)); console.log(isValiDate(str4)); |
true true true true false
Approach 5: Using Moment.js Library
Moment.js is a javascript library which is used to validate the Date. It has a function “moment” which can two aruments and it returns true if the date argument matches to the formate arument else it will return false.
Syntax :
moment(date , format).isValid();
OR
moment(date: String).isValid()
Example: This describes how to validate string date format using moment library in JavaScript.
Javascript
// Importing moment into js file const moment = require( 'moment' ); // Function for validation of date format function isValidDate(dateString, format) { return moment(dateString, format, true ).isValid(); } let Date1 = "2023-04-15" ; let Date2 = "15-04-2023" ; // Displaying the output // Output: true console.log(isValidDate(Date1, "YYYY-MM-DD" )); // Output: false console.log(isValidDate(Date2, "YYYY-MM-DD" )); |
Output
true
false