. Advertisement .
..3..
. Advertisement .
..4..
Do you know you can check if a date is valid in Javascript? If the answer is no, don’t worry. This guide will show you how to do so. Let’s get started!
Utilizing Date.parse() To Check If A Date Is Valid
To determine the validity of the date, you can use the method Date.parse(). Keep in mind that this Date.parse method only accepts the date as the single argument, which is the string.
Here is an example.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<h1 id="message"></h1>
<script>
let isValidDate = Date.parse('03/17/21');
if (isNaN(isValidDate)) {
document.getElementById('message').innerHTML = "This is not a valid date format.";
}
else{
document.getElementById('message').innerHTML = "This is a valid date format.";
}
</script>
</body>
</html>
Output.
This is a valid date format.
The method Date.parse gives back a millisecond timestamp in case the string contains a valid date. Suppose the date is invalid; the method will return a NaN value. The variable isValidDate holds the outcome of the function.
Check out this guide on converting timestamp to date if you need.
Utilizing moment.js To Check If A Date Is Valid
The best and simplest method for formatting and validating times and dates in JavaScript is to use the library moment.js.
This library must first be downloaded utilizing the command below before it can be used.
npm install -g moment --save
Now you may directly utilize the method moment() which is available in the library moment.js.
Here is an example as a reference.
import * as moment from 'moment';
let result = moment("05/22/12", 'MM/DD/YY',true).isValid();
console.log(result)
Output.
true
The function moment inputs consist of three parameters.
- First is the date that was entered and needs to be verified.
- Second is a format that you want to use for your date.
- Third is optional.
Notice that if it is true, the stringent parsing algorithm will be used. The input and format must match for the rigorous parsing to work, including the delimiters.
Now, utilize the function isValid() to determine the validity of the input date. This function gives back a true boolean value if the date matches the format supplied (dd/mm/yy). Otherwise, you will receive the false value.
Utilizing Regular Expressions To Check If A Date Is Valid
This is another method that can help you determine the validity of a date in format dd/mm/yy in Javascript. The only problem that you may have with regular expressions is that they can be confusing to understand because they involve a variety of numbers and symbols. Notice that each expression and symbol has a distinct meaning.
Observe the sample below.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
</head>
<body>
<h1 id="message"></h1>
<script>
var date_regex = /^(0[1-9]|1[0-2])\/(0[1-9]|1\d|2\d|3[01])\/(0[1-9]|1[1-9]|2[1-9])$/;
var testDate = "03/17/21"
if (date_regex.test(testDate)) {
document.getElementById("message").innerHTML = "Date follows dd/mm/yy format";
}
else{
document.getElementById("message").innerHTML = "Invalid date format";
}
</script>
</body>
</html>
Output.
Date follows dd/mm/yy format
The Bottom Line
Now you have all the steps for using JavaScript to check if a date is valid. Apply them now and get the results you are looking for.
Leave a comment