简体   繁体   中英

new Date().getTimezoneOffset() returns the wrong time zone

I am in Israel, so my offset right now should be 120 . Yet, when I use new Date().getTimezoneOffset() , I get back -120 , so not just a daylight savings issue. Should I simply change every minus to plus and vice versa? I don't know what might be the case for users in other timezones.

That's my function:

  firebase
    .database()
    .ref("words/" + newPostKey)
    .set({
      word,
      length: word.length,
      time_to_action: timeToAction,
      output: output,
      lang: project.lang,
      country: project.country,
      user: userUID,
      timestamp : Date.now(),
      timezone_offset : new Date().getTimezoneOffset()
    });

and timezone_offset is what gets saved as -120 . I am in Tel Aviv.

Well, I live in Ukraine and even if we have (UTC + 02: 00) now we are ahead of UTC by 3 hours cause we have 'winter time', when clocks are set back by one hour. So new Date().GetTimezoneOffset() returned '-120' , while new Date(0).getTimezoneOffset() returned '-180' . Maybe that's the case you faced with.

getTimezoneOffset() is working well. This function retuns the number of minutes you have to add to your current time to get the UTC time, so in GMT+X the result is negative.

Yes. You are ahead of GMT by 2 hours therefor to get back to GMT you have to subtract 120 minutes. Your offset is -120.

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