最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - new Date().getTimezoneOffset() returns the wrong time zone - Stack Overflow

programmeradmin3浏览0评论

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.

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.

Share Improve this question asked Feb 13, 2020 at 12:09 TsabaryTsabary 3,9985 gold badges40 silver badges92 bronze badges 1
  • 1 developer.mozilla/en-US/docs/Web/JavaScript/Reference/… “returns the time zone difference, in minutes, from current locale (host system settings) to UTC” - “I am in Israel, so my offset right now should be 120” - that is simply the other direction, from UTC to your locale. – misorude Commented Feb 13, 2020 at 12:12
Add a ment  | 

3 Answers 3

Reset to default 4

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.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论