suchen

Heim  >  Fragen und Antworten  >  Hauptteil

javascript - js-Datumskonvertierungs-Millisekundenproblem

Warum

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

Die Anzahl der zurückgegebenen Millisekunden ist unterschiedlich?

typechotypecho2845 Tage vor1000

Antworte allen(2)Ich werde antworten

  • 学习ing

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

    JavaScript的规范保证了new Date("2017-06-18")的行为。new Date("2017-6-18")不符合规格格式,浏览器可以做任何事情(包括在不同的时区解释);

    Chrome对待这些格式的解析产生差异 - firefox不会这样 - 所以建议使用规范中的日期格式,不要依赖浏览器处理任何“非标准”格式

    请按照规范写代码!

    Antwort
    0
  • 阿神

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

    new DateDate.parse 使用的是同样的解析规律,只是一个返回 Date object 另一个返回毫秒数,我们使用new Date来说明问题:

    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)

    刚好差8个小时,所以返回的毫秒数就不一样咯。

    Antwort
    0
  • StornierenAntwort