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

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

    returns 2016-02-16, midnight UTC, which is wrong, or at least not what I expected given what the other string parses as.

    It adds the timezone offset to the 00:00

    new Date("2016-02-16") outputs Tue Feb 16 2016 05:30:00 GMT+0530 (India Standard Time)

    My timezone being IST with an offset value (in minutes) +330, so it added 330 minutes to 00:00.

    As per ecma-262, section 20.3.3.2 Date.parse ( string )

    If ToString results in an abrupt completion the Completion Record is immediately returned. Otherwise, parse interprets the resulting String as a date and time; it returns a Number, the UTC time value corresponding to the date and time. The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String.

    When you explicitly set the time-units new Date("2016-02-16 00:00") it wll use set that as hours and minutes,

    Otherwise as stated here in 20.3.1.16

    If the time zone offset is absent, the date-time is interpreted as a local time.

提交回复
热议问题