Date.parse () doesn't work as expected in Mozilla Firefox JavaScript

I have a dateTime value of the string "01-01-2013 12:00:00 AM" and is parsed to a DateTime using Date.parse("01-01-2013 12:00:00 AM")

. This works fine in Google Chrome and IE browser. But doesn't work in Firefox . Anyone can help you parse this particular string to dateTime value in Mozilla Firefox .

Thank you Bharati.

+3


source to share


1 answer


TL; DR . You are using an invalid date format for this context, which Chrome and IE are just dealing with.

Full answer:

The specification only requires a JavaScript implementation to recognize certain formats in Date.parse

. In particular,

It accepts RFC2822 / IETF date syntax (RFC2822 section 3.3) for example. " Mon, 25 Dec 1995 13:30:00 GMT

". It understands continental US time abbreviations, but for general use use a time zone offset such as " Mon, 25 Dec 1995 13:30:00 +0430

" (4 hours, 30 minutes east of Greenwich meridian). If no time zone is specified and the string is in ISO format recognized by ES5, UTC is assumed. Greenwich Mean Time and UTC are considered equivalent. Local timezone is used to interpret arguments in RFC2822 section 3.3 (or any format not recognized in ES5 ISO 8601) that do not contain time zone information.

ECMAScript 5 ISO-8601 format support

The date time string can be in ISO 8601 format. For example, " 2011-10-10

" (date only) or " 2011-10-10T14:48:00

" (date and time) can be transmitted and parsed.



Your example,, 01-01-2013 12:00:00 AM

is not one of these formats. Some browsers might parse it anyway, depending on the JavaScript engine it uses, but it's non-standard. Chrome and IE recognize it, but Firefox returns NaN

, which is in line with the spec:

The ECMAScript specification states that if a string does not match in a standard format, the function can fall back to either implementation-specific heuristic, or parsing, implementation-specific algorithm. Unrecognizable strings or dates containing an illegal value element in ISO format strings should return Date.parse()

NaN

.

See this documentation for details .

+3


source







All Articles