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

后端 未结 5 566
慢半拍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:47

    Per DateParser::Parse() of V8 source codes for Chrome.

    ES5 ISO 8601 dates:

    [('-'|'+')yy]yyyy[-MM[-DD]][THH:mm[:ss[.sss]][Z|(+|-)hh:mm]]

    An unsigned number followed by ':' is a time value, and is added to the TimeComposer.

    timezone defaults to Z if missing

    > new Date("2016-02-16 00:00")
      Tue Feb 16 2016 00:00:00 GMT+0800 (China Standard Time)
    

    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.

    > new Date("2016-02-16")
    Tue Feb 16 2016 08:00:00 GMT+0800 (China Standard Time)
    

提交回复
热议问题