Reputation: 29693
Simple function Date.parse()
not working well in Internet Explorer 8.
I am using Date.parse()
to validate date in format "MM/DD/YYYY"
.
_.isNaN(Date.parse("99/99/9999"))
return true
- date is invalid _.isNaN(Date.parse("01/01/1990"))
return false
- date is valid But now I tried my JavaScript in IE 8 and I'm confused.
"88/88/8888"
- for this date all working well - date is invalid."13/35/2012"
- invalid date
but Date.parse("13/35/2012")
parse this date in IE only and don't return NaN
. Any ideas?
Upvotes: 3
Views: 6569
Reputation: 29693
I used my method for date validation
var isValidDate = function(dateAsString)
{
var parsedDate = Date.parse(dateAsString);
if (_.isNaN(parsedDate) || !_.isEqual(new Date(parsedDate).format("mm/dd/yyyy"), dateAsString))
{
return false
}
return true
}
Upvotes: 0
Reputation: 1977
Look here, here and here. In general Date.parse()
is not a cross browser solution. There are a lot of plugins and libraries available, just google it.
Upvotes: 1
Reputation: 413737
Standard JavaScript only accepts RFC 2822 dates, which don't look like that. You'll have to write your own code to separate out the date parts, convert them to numbers, and make Date
instances that way.
Internet Explorer also supports ISO dates (2012-09-20 08:22), and it will in fact parse "MM/DD/YYYY" dates. It's doing that for your "13/35/2012" date, which as far as JavaScript is concerned is a perfectly valid date: it's 04 February 2013. JavaScript "fixes" bogus dates; the 13th month of the year is the first month of the following year, and the 35th day of the month (if it's January, with 31 days) is the fourth day of the next month.
Basically you're expecting the Date parser to behave differently than it actually does.
Upvotes: 3