Using Moment
visit https://momentjs.com/docs/#/parsing/ for more details
Was able to find the solution. you can use a library called moment , only providing date to moment will validate it.
var date = moment("2016-10-19");
if you want to pass a date format you can do by specifying format in 2nd argument
var date = moment("2016-10-19", "YYYY-MM-DD");
And then date.isValid() gives the desired result.
To test if a value is a date in JavaScript, you can use the Date.parse()
method. The Date.parse()
method attempts to parse a date string and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC. If the date string is not valid, it will return NaN
.
You can use the following function to check if a value is a valid date:
function isDate(value) {
const timestamp = Date.parse(value);
return !isNaN(timestamp);
}
Now you can use the isDate()
function to check if a value is a date. It will return true
if the value is a valid date, and false
otherwise.
Here are some examples:
console.log(isDate("MOD-001-01")); // false
console.log(isDate("This is not a date 1")); // false
console.log(isDate("2023-07-11")); // true
console.log(isDate("July 11, 2023")); // true
By using Date.parse()
, you can reliably determine if a value is a date or not.
or else
You can use a regular expression to check if a value is a date.
You can use a library like Moment.js to parse and manipulate dates. These libraries provide more robust date parsing and manipulation capabilities than the built-in Date object.
pls upvote if you find this helpful