Why does the same date have different hours?

前端 未结 3 1941
余生分开走
余生分开走 2021-01-22 20:16

Maybe the answer is obvious, but I don\'t get it. Why are the Dates in the Code Snippet different? Does the format say something about the hours as well?

相关标签:
3条回答
  • 2021-01-22 20:19

    In some browsers, months or days with no leading zeroes may produce an error:

    new Date("2017-2-9");
    

    In consquent, that the behavior of the format "yyyy-mm-dd" is undefined.

    Some browsers will try to guess the format, some will return NaN and some will return null.

    That is why new Date("2017-02-09") has Thu Feb 09 2017 01:00:00 GMT+0100 (Mitteleuropäische Zeit) as output, because the behavior for this format is defined and it adds the timezone to the date. new Date("2017-2-9") has Thu Feb 09 2017 00:00:00 GMT+0100 (Mitteleuropäische Zeit) as output, because chrome trys to guess the format, but cannot add the timezone. In Safari in return null.

    0 讨论(0)
  • 2021-01-22 20:34

    TL;DR: Because the language specification says that date strings not conforming to the specified format can be parsed according to "any implementation-specific heuristics or implementation-specific date formats," and YYYY-M-D is just such a string.

    Let's dive into the spec. Here's what the ECMAScript 5.1 spec says about the Date constructor (I'm quoting it instead of the current, ES2016 spec just because it's simpler, but the latter works basically the same in this case):

    15.9.3.2 new Date (value)

    ...

    The [[PrimitiveValue]] internal property of the newly constructed object is set as follows:

    1. Let v be ToPrimitive(value).
    2. If Type(v) is String, then

      • Parse v as a date, in exactly the same manner as for the parse method (15.9.4.2); let V be the time value for this date.
    3. ...

    And here's the spec for parse (emphasis mine):

    15.9.4.2 Date.parse (string)

    The parse function applies the ToString operator to its argument and interprets the resulting String as a date and time... 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. ...

    Date Time String Format, in a nutshell, is YYYY-MM-DDTHH:mm:ss.sssZ and its subsets. Since YYYY-M-D doesn't conform to that format, the interpreter is (unfortunately) free to do whatever it wants. If you want to know why Chrome does it in this particular way, you'll have to dig around in the V8 source.

    0 讨论(0)
  • 2021-01-22 20:41

    The dates being displayed are using different timezones. The Chrome console output is using your browsers local timezone and adjusting it appropriately. The (GMT+0100) is telling you which timezone and adjustments that are being made. The code snippet console is displaying using UTC. The 'Z' at the end of the string signifies that.

    If you want to convince yourself of that they are the same, print the timestamp for each date also. This is also why when you are dealing with dates, you should rely on timestamps rather than comparing dates like this. Things get very confusing and difficult to deal with.

    0 讨论(0)
提交回复
热议问题