date.getTimezoneOffset() is not returning an expected value

随声附和 提交于 2019-12-11 11:57:27

问题


My browser is running in the Eastern Standard Timezone, when call I call date.getTimezoneOffset() I expect -300 to be returned but instead I get 300

var date = new Date();
date.getTimezoneOffset();
// returns 300

回答1:


From the Mozilla docs (or devdocs.io):

Return value
The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight saving time prevents this value from being a constant even for a given locale.

EST is behind UTC, therefore you're getting a positive result.

I agree it's not a great way to represent it - I'm much more used to an offset being "the amount of time you add to UTC to get local time" but it is at least behaving as documented...




回答2:


It's the difference in minutes from UTC to the timezone you're in. UTC - EST = 300.

From MDN's reference description of the function:

The time-zone offset is the difference, in minutes, between UTC and local time. Note that this means that the offset is positive if the local timezone is behind UTC and negative if it is ahead. For example, if your time zone is UTC+10 (Australian Eastern Standard Time), -600 will be returned. Daylight saving time prevents this value from being a constant even for a given locale.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/getTimezoneOffset



来源:https://stackoverflow.com/questions/33659991/date-gettimezoneoffset-is-not-returning-an-expected-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!