javascript - The problem of converting JS string to timestamp with a difference of 8 hours
怪我咯
怪我咯 2017-05-19 10:18:39
0
3
792

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?

怪我咯
怪我咯

走同样的路,发现不同的人生

reply all(3)
黄舟

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]);
}
某草草

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-...

小葫芦

It’s normal under Firefox

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