如何获取时区的原始偏移?Node.JS

huangapple go评论97阅读模式
英文:

How to get timezone RAW Offset? Node.JS

问题

When you go to https://worldtimeapi.org/api/timezone/Europe/Berlin it says raw_offset=3600, I'm trying to get this value in Node.JS. Need help please, I'm giving my code below but it's giving me the wrong output.

  1. const moment = require("moment-timezone");
  2. let timezone = "Europe/Warsaw";
  3. let haha = moment().tz(timezone);
  4. let timezone_raw = haha.utcOffset();
英文:

When you go to https://worldtimeapi.org/api/timezone/Europe/Berlin it says raw_offset=3600 , i'm trying to get this value in Node.JS. Need help please, i'm giving my code below but it's giving me wrong output

  1. const moment = require("moment-timezone");
  2. let timezone = "Europe/Warsaw";
  3. let haha = moment().tz(timezone);
  4. let timezone_raw = haha.utcOffset()
  5. </details>
  6. # 答案1
  7. **得分**: 0
  8. 看起来并没有错,只是它们使用不同的格式。
  9. Worldtime-API 似乎使用秒,而 moment.js 似乎使用分钟。
  10. 根据我的测试,moment.js 也从 `utcOffset()` 返回 60
  11. 您可以将 `raw_offset` 除以 60(每分钟的秒数)得到 60(分钟)。
  12. 或者您可以取 `utcOffset()` 的返回值,乘以 60 得到 3600
  13. <details>
  14. <summary>英文:</summary>
  15. Doesn&#39;t look wrong they just use a different format.
  16. Worldtime-API seems to use seconds where moment.js seems to use minutes.
  17. According to my test moment.js also returns 60 from ``utcOffset()``.
  18. You can either take the ``raw_offset`` and divide it by 60 (seconds per minute) to get 60 (minutes).
  19. Or you can take the return from ``utcOffset()`` and multiply it by 60 to get 3600.
  20. </details>

huangapple
  • 本文由 发表于 2023年1月6日 03:17:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75023360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定