I encountered an issue while attempting to validate a date string using the following code:
const isValidDate = (date: any) => {
return (new Date(date) !== "Invalid Date") && !isNaN(new Date(date));
}
For instance:
let dateStr = "some-random-string-09"
console.log(isValidDate(dateStr)) // returns true instead of false
Oddly enough, removing the '09' from the string or adding extra text at the end yields the expected result.
For example:
let dateStr = "some-random-string"
console.log(isValidDate(dateStr)) // returns false as expected
This behavior is quite perplexing. Is there a solution to validate this specific type of string in TypeScript?