Question

Why Date.parse("2011-11-15") considers current time zone in Web browser, and Date.parse("2011/11/15") does not? Why the results are different?

Was it helpful?

Solution

The first form is being considered as an ISO date in UTC, according to section 15.9.1.15 of ECMA-262. The second form is being considered in an implementation-specific way, as per section 15.9.4.2:

The parse function applies the ToString operator to its argument and interprets the resulting String as a date and time; it returns a Number, the UTC time value corresponding to the date and time. 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

So I suspect "2011/11/15" is being converted to the local midnight of November 15th, whereas "2011-11-15" is being converted to UTC midnight of November 15th.

OTHER TIPS

from date.parse documentation

the date/time string may be in ISO 8601 format. Starting with JavaScript 1.8.5 / Firefox 4, a subset of ISO 8601 is supported. For example, "2011-10-10" (just date) or "2011-10-10T14:48:00 (date and time) can be passed and parsed. Timezones in ISO dates are not yet supported, so e.g. "2011-10-10T14:48:00+0200" (with timezone) does not give the intended result yet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top