In a project I am working on, I need to validate a date entered into an <input type="date">
With Safari 4/5 (on Mac OSX) the Javascript fails to parse dates of the format YYYY-MM-DD
, returning NaN
instead of the expected epoch timestamp.
I am using the following technique to validate the field just before the form is submitted:
//value = '2010-06-21'
var stamp = Date.parse(value);
if (isNaN(stamp)) {
//notify user
} else {
value = new Date(stamp).format_mysql();
}
Where format_mysql()
is a prototype function that formats the date (correctly) into MySQL Date-Time format (YYYY-MM-DD).
Replacing the -
's with /
's (YYYY/MM/DD) yields a "correct" timestamp.
I should note that the field should accept any date format, not just YYYY-MM-DD, and that though I would like to, I cannot use libraries like Date.js
How can I fix this, or is there a better way to parse/validate a date?