Why isn't “2016-02-16” equal to “2016-02-16 00:00”?

后端 未结 5 564
慢半拍i
慢半拍i 2020-11-30 01:54

I\'m trying to pass both date strings to new Date(t).

I expect both strings represent the same time, after all, if I omit the time, shouldn\'t it be mid

5条回答
  •  臣服心动
    2020-11-30 02:46

    You are perhaps running into a differences between ES5, ES6 implementations and your expected result. Per Date.parse at MDN, "especially across different ECMAScript implementations where strings like "2015-10-12 12:00:00" may be parsed to as NaN, UTC or local timezone" is significant.

    Additional testing in Firefox 44 and IE 11 revealed they both return a date object for new Date("2016-02-16 00:00"), which object returns NaN when atttempting to get a date component value, and whose toString value is "Invalid Date" (not "NaN"). Hence appending " 00:00 to get consistent behavior" can easily break in different browsers.

    As noted in other answers new Date("2016-02-16") uses a timezone offset of zero by default, producing midnight UTC instead of local.

提交回复
热议问题