简体   繁体   English

new Date().getTimezoneOffset() 返回错误的时区

[英]new Date().getTimezoneOffset() returns the wrong time zone

I am in Israel, so my offset right now should be 120 .我在以色列,所以我现在的偏移量应该是120 Yet, when I use new Date().getTimezoneOffset() , I get back -120 , so not just a daylight savings issue.然而,当我使用new Date().getTimezoneOffset()时,我返回-120 ,所以不仅仅是夏令时问题。 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:那是我的 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 . timezone_offset是保存为-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.好吧,我住在乌克兰,即使我们有 (UTC + 02: 00) 现在我们也比 UTC 提前 3 小时,因为我们有“冬令时”,时钟拨慢一小时。 So new Date().GetTimezoneOffset() returned '-120' , while new Date(0).getTimezoneOffset() returned '-180' .所以new Date().GetTimezoneOffset()返回'-120' ,而new Date(0).getTimezoneOffset()返回'-180' Maybe that's the case you faced with.也许这就是你面临的情况。

getTimezoneOffset() is working well. getTimezoneOffset()运行良好。 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.此函数返回您必须添加到当前时间以获得 UTC 时间的分钟数,因此在 GMT+X 中,结果为负数。

Yes.是的。 You are ahead of GMT by 2 hours therefor to get back to GMT you have to subtract 120 minutes.您比格林威治标准时间早 2 小时,因此要回到格林威治标准时间,您必须减去 120 分钟。 Your offset is -120.您的偏移量为 -120。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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