Date constructor: numeric arguments vs. string argument giving different dates in some cases

后端 未结 5 1945
孤街浪徒
孤街浪徒 2021-01-01 16:18

First of all, I think timezone probably has something to do with this. I\'m in EST/EDT. Also, I\'m testing this on chromium 17 / linux.

Now, let\'s say I create two

5条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-01 16:29

    After looking in V8's source code:

    // Specification:
    // Accept ES5 ISO 8601 date-time-strings or legacy dates compatible
    // with Safari.
    <...>
    //  A string that matches both formats (e.g. 1970-01-01) will be
    //  parsed as an ES5 date-time string - which means it will default
    //  to UTC time-zone. That's unavoidable if following the ES5
    //  specification.
    

    Reading the surrounding code, it seems that a date-time string with both month and day 2 symbols long is considered a valid ES5 date-time string. Further down in the ES5 parser, after it parses the dates and times, there's a comment:

    // Successfully parsed ES5 Date Time String. Default to UTC if no TZ given.
    

    In case of "YYYY-MM-DD", once the code gets that far, the ES5 parser has successfully parsed the entire string, so it returns before the legacy parser gets a chance to localize the timezone. Otherwise (month/day are one symbol long) it's treated as a "legacy" datetime and the legacy parser gets to deal with it and localize it.

提交回复
热议问题