Home  >  Q&A  >  body text

javascript - The problem of converting JS string to timestamp with a difference of 8 hours

var time = new Date('2014-07-03'); //Thu Jul 03 2014 08:00:00 GMT 0800 (China Standard Time)
Date.parse(time); //1404345600000


var time = new Date('2014-7-3'); //Thu Jul 03 2014 00:00:00 GMT 0800 (China Standard Time)
Date.parse(time); //1404316800000


How come there are 8 more hours after completing 0?

怪我咯怪我咯2734 days ago776

reply all(3)I'll reply

  • 黄舟

    黄舟2017-05-19 10:20:39

    According to ECMA-262 ed 3, time parsing may be handled differently.

    In ES5, strings in ISO 8601 format, if no time zone is specified, are parsed into UTC, and there is no unified behavior for processing non-ISO 8601 strings.

    In ES2015, strings without specified time zone are parsed to local time (according to system time).

    If you want to process all strings in this form as local time, you can write a function yourself:

    function parseISOLocal (s) {
      var b = s.split(/\D/);
      return new Date(b[0], b[1]-1, b[2]);
    }

    reply
    0
  • 某草草

    某草草2017-05-19 10:20:39

    I also saw this problem some time ago.

    If you change the date format to "2014/07/02", it will be converted to 0 o'clock normally.

    Reference: Xuanfengge http://www.xuanfengge.com/js-...

    reply
    0
  • 高洛峰

    高洛峰2017-05-19 10:20:39

    It’s normal under Firefox

    reply
    0
  • Cancelreply