Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

javascript

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.

like image 419
Tsabary Avatar asked Oct 19 '25 01:10

Tsabary


2 Answers

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.

like image 168
RaptorDeveloper Avatar answered Oct 21 '25 14:10

RaptorDeveloper


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.

like image 30
ojovirtual Avatar answered Oct 21 '25 16:10

ojovirtual