简体   繁体   中英

Chrome Javascript Date Constructor bugs?

I need to calculate a date and wrote the next.

new Date(service.today().getTime() + (service.plazo * service.DAY_MILLISECONDS))

service.today = function() {
  var now = new Date();
  now.setHours(0,0,0,0);
  return now;
};

service.DAY_MILLISECONDS = 60*60*24*1000;

service.plazo is a number (1,2,3,4....)

the result

Wed May 18 2016 23:00:00 GMT-0300 (Hora est. Sudamérica Pacífico)

i changed my timezone in my laptop: -3 to -4

Wed May 18 2016 23:00:00 GMT-0400 (Hora est. Sudamérica Pacífico)

all perfect BUT

new Date()

Thu May 12 2016 16:19:51 GMT-0300 (Hora verano Sudamérica Pacífico)

new Date(service.today().getTime() + (service.plazo * service.DAY_MILLISECONDS))

Wed May 18 2016 23:00:00 GMT-0400 (Hora est. Sudamérica Pacífico)

new Date(service.today().getTime())

Thu May 12 2016 00:00:00 GMT-0300 (Hora verano Sudamérica Pacífico)

new Date(service.plazo * service.DAY_MILLISECONDS)

Wed Jan 07 1970 21:00:00 GMT-0300 (Hora verano Sudamérica Pacífico)

Why

  • new Date()"
  • new Date(service.today().getTime())
  • new Date(service.plazo * service.DAY_MILLISECONDS)

is GMT-0300 and

  • new Date(service.today().getTime() + (service.plazo * service.DAY_MILLISECONDS))

is GMT-0400

Chrome Version with error: 50.0.2661.94 m

Chrome Version 46.0.2490.86 (64-bit) all is GMT-0400 :)

Because of the time changes (standard time to daylight saving time), the timeZoneOffset will vary depending on which period the date you get falls.

I'm guessing daylight saving time ends may 14 in your country. This means that a date before May 14 will be GMT -0300, but with Hora verano Sudamérica Pacífico , and after you'll have -0400 but with Hora est. Sudamérica Pacífico .

This way, you'll get the time that was the current time in your country at the specific date you're getting. The time changes date and in general the timezone info are OS dependent, Chrome relies on your system settings to determine them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM