Although it's commonly advised against creating a date from a string, I stumbled upon an interesting phenomenon: adding a space before or after the date string can impact the resulting date value.
console.log([
new Date('2019-03'), // 2019-03-01T00:00:00.000Z
new Date('2019-03 '), // 2019-02-28T23:00:00.000Z
new Date(' 2019-03'), // 2019-02-28T23:00:00.000Z
new Date('2019-03-05'), // 2019-03-05T00:00:00.000Z
new Date('2019-03-05 '), // 2019-03-04T23:00:00.000Z
new Date('2019/04/16'), // 2019-04-15T22:00:00.000Z
new Date('2019/04/16 '), // 2019-04-15T22:00:00.000Z
]);
As stated in the Date
documentation, using new Date(<string>)
internally calls Date.parse
to extract the time value. However, there is no explicit mention of how untrimmed strings are handled.
This issue has me puzzled! Why would something as trivial as spacing affect the generated time? This is programming, not theoretical physics!
The displayed console outputs were generated by a Chrome 73 browser powered by a v8 engine in Berlin (UTC+1)