Reputation: 1328
I have the string dateTime value "01-01-2013 12:00:00 AM" and parsed to DateTime using Date.parse("01-01-2013 12:00:00 AM")
. This is working fine in Google Chrome and IE browser. But not working in Firefox. Anyone help how to parse this specific string to dateTime value in Mozilla Firefox.
Thanks, Bharathi.
Upvotes: 4
Views: 2109
Reputation: 44833
TL;DR You're using an invalid date format for this context, which Chrome and IE just happen to handle.
Full answer:
The specification only requires a JavaScript implementation to recognize certain formats in Date.parse
. Specifically,
It accepts the RFC2822 / IETF date syntax (RFC2822 Section 3.3), e.g. "
Mon, 25 Dec 1995 13:30:00 GMT
". It understands the continental US time zone abbreviations, but for general use, use a time zone offset, for example, "Mon, 25 Dec 1995 13:30:00 +0430
" (4 hours, 30 minutes east of the Greenwich meridian). If a time zone is not specified and the string is in an ISO format recognized by ES5, UTC is assumed. GMT and UTC are considered equivalent. The local time zone is used to interpret arguments in RFC2822 Section 3.3 format (or any format not recognized as ISO 8601 in ES5) that do not contain time zone information.ECMAScript 5 ISO-8601 format support
The date time string may be in ISO 8601 format. For example, "
2011-10-10
" (just date) or "2011-10-10T14:48:00
" (date and time) can be passed and parsed.
Your example, 01-01-2013 12:00:00 AM
, is not one of those formats. Some browsers may parse it anyway, depending on the JavaScript engine they are using, but it's non-standard. Chrome and IE happen to recognize it, but Firefox returns NaN
, which is compliant with the spec:
The ECMAScript specification states: If the String does not conform to the standard format the function may fall back to any implementation–specific heuristics or implementation–specific parsing algorithm. Unrecognizable strings or dates containing illegal element values in ISO formatted strings shall cause
Date.parse()
to returnNaN
.
See this documentation for more details.
Upvotes: 5