1

After reading this question - which also provides links to documentation , I still have a question about the documentation.

MDN : Date.parse

A string representing an RFC2822 or ISO 8601 date.

Ok , RFC2282 :

date-time       =       [ day-of-week "," ] date FWS time [CFWS]
day-of-week     =       ([FWS] day-name) / obs-day-of-week
day-name        =       "Mon" / "Tue" / "Wed" / "Thu" /
                       "Fri" / "Sat" / "Sun"

date            =       day month year
year            =       4*DIGIT / obs-year
month           =       (FWS month-name FWS) / obs-month
month-name      =       "Jan" / "Feb" / "Mar" / "Apr" /
                        "May" / "Jun" / "Jul" / "Aug" /
                        "Sep" / "Oct" / "Nov" / "Dec"

day             =       ([FWS] 1*2DIGIT) / obs-day

Now - ISO8601

 Year:
      YYYY (eg 1997)
   Year and month:
      YYYY-MM (eg 1997-07)
   Complete date:
      YYYY-MM-DD (eg 1997-07-16)

Ok.

Questions :

  • MDN provided a pattern sample (which is working) "Dec 25, 1995" which is not found in 2282 nor in ISO . How come this sample works ? the order must be day month year ( according to 2282) .

  • the separators in the standards are [space] ( in 2282) and [-] in ISO.

    So why this sample works ( cross browser) ? Date.parse("2011/11/23")

Community
  • 1
  • 1
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

1 Answers1

1

For Date.parse, have a look at the EcmaScript specification, section 15.9.4.2:

The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

So, MDN's Mozilla-specific documentation is quite near that. The official "EcmaScript Date Time String Format" is a subset of ISO 8601, otherwise Gecko browsers try to interpret it as RFC 2822 or even something else (this might include "Dec 25, 1995"). I strongly suspect that "2011/11/23" really works cross-browser, although it might be true for recent versions.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
  • too abstract : "any implementation-specific heuristics ...". what are the other formats? where can i see them ? – Royi Namir Dec 08 '12 at 19:05
  • Yes, it is too abstract. Every browser implements its own formats, and old IEs do not even parse the EcmaScript Format correctly. – Bergi Dec 08 '12 at 19:07
  • @RoyiNamir: I'm getting `1353625200000`, obviously due to different timezone settings. – Bergi Dec 08 '12 at 19:14
  • what time zone problems ? it is a valid date. and the separators are ok.. (ISO). – Royi Namir Dec 08 '12 at 19:16
  • Not really problems, just different timezone settings (edited comment). Sure, it parses correctly, but does not return the UTC date – Bergi Dec 08 '12 at 19:19
  • im FF its ok. the prob is with IE – Royi Namir Dec 08 '12 at 19:20
  • On Internet Explorer: It does provide a spec-compliant parse (for ISO dates) only since version 9 (non-quirks), see http://msdn.microsoft.com/en-us/library/ie/ff743760(v=vs.94).aspx – Bergi Dec 08 '12 at 19:20
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20808/discussion-between-royi-namir-and-bergi) – Royi Namir Dec 08 '12 at 19:21