I know that creating a date from string is usually a bad idea, but still, this caught my attention: adding a space before or after the date string can affect the created 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
]);
According to the Date
docs, new Date(<string>)
invokes Date.parse
to get the time value. Besides that, the docs don't seem to give any pointers to what happens to untrimmed strings.
I'm really stuck on this one! Why do space affect the time? It's programming, not general relativity!
The console logs above where produced by a Chrome 73 browser running a v8 engine in Berlin (UTC+1)