date.getTimezoneOffset()未返回期望值 [英] date.getTimezoneOffset() is not returning an expected value

查看:66
本文介绍了date.getTimezoneOffset()未返回期望值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的浏览器在东部标准时区运行,当我打电话给date.getTimezoneOffset()时,我希望返回-300却得到300

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


推荐答案

Mozilla文档(或 devdocs.io ):


返回值

时区偏移量是UTC与本地时间之间的差,以分钟为单位。 请注意,这意味着如果本地时区位于UTC之前,则偏移量为正;如果位于本地时区之前,则为负值。例如,如果您的时区为UTC + 10(澳大利亚东部标准时间),则-600将被退回。夏时制即使在给定的语言环境下,也无法使该值保持不变。

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是 behind UTC ,因此您得到了积极的结果。

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

我同意这不是代表它的好方法-我更习惯于抵消时间添加到UTC以获取本地时间,但至少表现得如所记载...

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...

这篇关于date.getTimezoneOffset()未返回期望值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆