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

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

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.

const moment = require("moment-timezone");
let timezone = "Europe/Warsaw";
let haha = moment().tz(timezone);
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

const moment = require("moment-timezone");
let timezone = "Europe/Warsaw";
let haha = moment().tz(timezone);
let timezone_raw = haha.utcOffset()

</details>


# 答案1
**得分**: 0

看起来并没有错,只是它们使用不同的格式。
Worldtime-API 似乎使用秒,而 moment.js 似乎使用分钟。
根据我的测试,moment.js 也从 `utcOffset()` 返回 60。

您可以将 `raw_offset` 除以 60(每分钟的秒数)得到 60(分钟)。
或者您可以取 `utcOffset()` 的返回值,乘以 60 得到 3600。

<details>
<summary>英文:</summary>

Doesn&#39;t look wrong they just use a different format.
Worldtime-API seems to use seconds where moment.js seems to use minutes.
According to my test moment.js also returns 60 from ``utcOffset()``.

You can either take the ``raw_offset`` and divide it by 60 (seconds per minute) to get 60 (minutes). 
Or you can take the return from ``utcOffset()`` and multiply it by 60 to get 3600.

</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:

确定