This answer gives a good way to check for dates in JavaScript:
function isValidDate(d) {
return d instanceof Date && !isNaN(d);
}
However, this isn't working for me in Chrome because it identifies strings that contain numbers as dates:
function getDate(str) {
console.log(new Date(str).toLocaleString());
}
getDate("a string that contains the number 1");
getDate("a string that contains the number 12");
getDate("a string that contains the number 123");
getDate("a string that contains the number 1234");
getDate("a string that contains the number 12345");
getDate("a string that contains the number 123456");
getDate("a string that contains the number 1234567"); // in Chrome, only this one returns "Invalid Date"
What would be the best way to implement a stricter date checker that would work with Chrome's flexible date parsing? I cannot use anything that requires the dates to be formatted in a specific way (e.g., that only accepts ISO 8601 formats).