The JavaScript specification guarantees the behavior of new Date("2017-06-18"). new Date("2017-6-18") does not conform to the specification format and the browser can do anything (including interpreting in different time zones);
Chrome treats the parsing of these formats differently - Firefox does not - so it is recommended to use the date formats in the specification and not rely on the browser to handle any "non-standard" formats
Please write code according to the specifications!
new Date and Date.parse use the same parsing rules, except that one returns a Date object and the other returns milliseconds. We use new Date to illustrate the problem:
console.log(new Date('2017-06-18')) // Sun Jun 18 2017 08:00:00 GMT+0800 (CST)
console.log(new Date('2017-6-18')) // Sun Jun 18 2017 00:00:00 GMT+0800 (CST)
It’s exactly 8 hours different, so the number of milliseconds returned is different.
The JavaScript specification guarantees the behavior of new Date("2017-06-18"). new Date("2017-6-18") does not conform to the specification format and the browser can do anything (including interpreting in different time zones);
Chrome treats the parsing of these formats differently - Firefox does not - so it is recommended to use the date formats in the specification and not rely on the browser to handle any "non-standard" formats
Please write code according to the specifications!
new Date
andDate.parse
use the same parsing rules, except that one returns aDate object
and the other returns milliseconds. We usenew Date
to illustrate the problem:It’s exactly 8 hours different, so the number of milliseconds returned is different.