search

Home  >  Q&A  >  body text

javascript - js Date conversion milliseconds problem

Why

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

Are the milliseconds returned different?

typechotypecho2727 days ago931

reply all(2)I'll reply

  • 学习ing

    学习ing2017-06-12 09:32:26

    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!

    reply
    0
  • 阿神

    阿神2017-06-12 09:32:26

    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.

    reply
    0
  • Cancelreply