admin管理员组文章数量:1323200
I have a JSON response from my server giving me UTC Unix timestamps in seconds. I'm parsing that into JavaScript dates that will be used in a chart (displaying the time in the user's locale).
I obviously have to coax the timestamp I have (in UTC) into the browser's locale, so I wrote a function that creates a new Date
in the browser's locale, calls getTimezoneOffset()
on it to get the "offset in minutes" in the current locale, as per the MDN, converts both to milliseconds, and returns the sum. Now I have a JavaScript friendly Unix timestamp in the user's locale.
However, I don't.
as it turns out, (new Date()).getTimezoneOffset()
returns (positive) 300 in GMT-5 and -120 in GMT+2. Why is the offset inverted? I would have expected the offset to match the sign of the timezone - ie: I need to subtract 300 minutes to get to GMT-5, and ADD 120 minutes to get to GMT+2. Instead, I have to subtract the values that are returned by getTimezoneOffset
I have a JSON response from my server giving me UTC Unix timestamps in seconds. I'm parsing that into JavaScript dates that will be used in a chart (displaying the time in the user's locale).
I obviously have to coax the timestamp I have (in UTC) into the browser's locale, so I wrote a function that creates a new Date
in the browser's locale, calls getTimezoneOffset()
on it to get the "offset in minutes" in the current locale, as per the MDN, converts both to milliseconds, and returns the sum. Now I have a JavaScript friendly Unix timestamp in the user's locale.
However, I don't.
as it turns out, (new Date()).getTimezoneOffset()
returns (positive) 300 in GMT-5 and -120 in GMT+2. Why is the offset inverted? I would have expected the offset to match the sign of the timezone - ie: I need to subtract 300 minutes to get to GMT-5, and ADD 120 minutes to get to GMT+2. Instead, I have to subtract the values that are returned by getTimezoneOffset
-
See the description here.
This means that the offset is positive if the local timezone is behind UTC and negative if it is ahead.
– Jashwant Commented Feb 3, 2013 at 4:35 - so if I'd actually clicked through to the method...... facepalm – Thomas Jones Commented Feb 3, 2013 at 4:37
1 Answer
Reset to default 9Nope.
The spec (§15.9.5.26) says:
15.9.5.26 Date.prototype.getTimezoneOffset ( )
Returns the difference between local time and UTC time in minutes.
- Let t be this time value.
- If t is NaN, return NaN.
- Return (t − LocalTime(t)) / msPerMinute.
本文标签: javascriptDategetTimezoneOffset invertedStack Overflow
版权声明:本文标题:javascript - Date.getTimezoneOffset inverted? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742094728a2420478.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论