在Javascript中从时区获取utc偏移量 [英] Get utc offset from timezone in Javascript

查看:437
本文介绍了在Javascript中从时区获取utc偏移量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个给出时区的Javascript函数,返回当前的utc偏移量。

I need a Javascript function that given a timezone, returns the current utc offset.

例如,theFuncIneed('US / Eastern') - > 240

For example, theFuncIneed('US/Eastern') -> 240

任何想法?

谢谢

推荐答案

一般情况下,这是不可能的。


  • US / Eastern 时区的标识符。 (它实际上是 America / New_York 的别名,这是真正的标识符。)

  • US/Eastern is an identifier for a time zone. (It's actually an alias to America/New_York, which is the real identifier.)

240 是时区 offset 。它通常写成 -04:00 (反转符号,除以60)。

240 is a time zone offset. It's more commonly written as -04:00 (Invert the sign, divide by 60).

美国东部时区由东部标准时间组成,其偏移量 -05:00 和东部夏令时间,偏移量 -04:00

The US Eastern Time Zone is comprised of both Eastern Standard Time, which has the offset of -05:00 and Eastern Daylight Time, which has the offset of -04:00.

所以说不准确美国/东部 = 240.请阅读时区标签wiki ,特别是标题为时区!=偏移的部分。

So it is not at all accurate to say US/Eastern = 240. Please read the timezone tag wiki, especially the section titled "Time Zone != Offset".

现在您确实要求当前偏移量,其中可能。如果您提供日期+时间参考,则可以解决此问题。

Now you did ask for the current offset, which is possible. If you supply a date+time reference, then you can resolve this.


  • 对于计算机的本地时区javascript代码正在执行,它内置于 .getTimeZoneOffset(),来自 Date 对象的任何实例。

  • For the local time zone of the computer where the javascript code is executing, this is built in with .getTimeZoneOffset() from any instance of a Date object.

但是如果你想要它用于特定的时区,那么你需要使用我在此列出的其中一个库

But if you want it for a specific time zone, then you will need to use one of the libraries I listed here.

这篇关于在Javascript中从时区获取utc偏移量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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