Why does the same date have different hours?

前端 未结 3 1942
余生分开走
余生分开走 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.

提交回复
热议问题