javascript - js Date conversion milliseconds problem
typecho
typecho 2017-06-12 09:30:26
0
2
940

Why

Date.parse('2017-06-18');    //1497744000000
Date.parse('2017-6-18');    //1497715200000

Are the milliseconds returned different?

typecho
typecho

Following the voice in heart.

reply all(2)
学习ing

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.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template