I was trying to check if a string is valid date string or not with below code
const isValidDate = (date: any) => {
return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date));
}
For example :
let dateStr = "some-random-string-09"
console.log(isValidDate(dateStr)) // returns true instead of false
But if I remove the 09
from the string or add some text at the end of the string it returns the expected result.
for Ex:
let dateStr = "some-random-string"
console.log(isValidDate(dateStr)) // returns false as expected
it is really strange behaviour. Is there a way to validate this particular type of string in TypeScript?