如何将JavaScript日期初始化为特定时区 [英] How to initialize a JavaScript Date to a particular time zone

查看:155
本文介绍了如何将JavaScript日期初始化为特定时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将特定时区的日期时间作为字符串,我想将其转换为当地时间。但是,我不知道如何在Date对象中设置时区。

I have date time in a particular timezone as a string and I want to convert this to the local time. But, I don't know how to set the timezone in the Date object.

例如,我有 2013年2月28日下午7:00 ET,然后我可以

var mydate = new Date();
mydate.setFullYear(2013);
mydate.setMonth(02);
mydate.setDate(28);
mydate.setHours(7);
mydate.setMinutes(00);  

据我所知,我可以设置UTC时间或当地时间。但是,我如何在另一个时区设置时间?

As far as I know, I can either set the UTC time or local time. But, how do I set time in another timezone?

我试图使用添加/减去UTC的偏移但我不知道如何应对夏令时。我不确定我是否正朝着正确的方向前进。

I tried to use the add/subtract the offset from UTC but I don't know how to counter daylight savings. Am not sure if I am heading the right direction.

如何在javascript中将时间从不同的时区转换为本地时间?

How can I go about converting time from a different timezone to local time in javascript?

推荐答案

背景



JavaScript的日期对象在内部跟踪UTC时间,但通常接受输入和在其运行的计算机的本地时间输出。它没有任何在其他时区使用时间的设施。它可以解析并输出UTC或Local的日期,但 它不能直接用于其他时区

Background

JavaScript's Date object tracks time in UTC internally, but typically accepts input and output in the local time of the computer it's running on. It doesn't have any facilities for working with time in other time zones. It can parse and output dates that are UTC or Local, but it can't directly work with other time zones.

绝对准确地说, Date 对象的内部表示是一个数字,表示自 1970-以来经过的毫秒数01-01 00:00:00 UTC ,不考虑闰秒。 Date对象本身没有存储时区或字符串格式。当使用 Date 对象的各种函数时,计算机的本地时区适用于内部表示。如果函数生成字符串,则可以考虑计算机的区域设置信息以确定如何生成该字符串。细节因功能而异,有些是特定于实现的。

To be absolutely precise, the internal representation of a Date object is a single number, representing the number of milliseconds that have elapsed since 1970-01-01 00:00:00 UTC, without regard to leap seconds. There is no time zone or string format stored in the Date object itself. When various functions of the Date object are used, the computer's local time zone is applied to the internal representation. If the function produces a string, then the computer's locale information may be taken into consideration to determine how to produce that string. The details vary per function, and some are implementation-specific.

幸运的是,有些库可以可用于处理时区。虽然它们仍然无法使 Date 对象的行为有所不同,但它们通常实现标准的Olson / IANA时区数据库,并提供在JavaScript中使用它的功能。如果您在Web浏览器中运行,有些会产生开销,因为如果您想要整个数据库,数据库可能会变得有点大。幸运的是,许多这些库允许您有选择地选择要支持的区域,使数据大小更加可口。还有一些使用现代功能从 Intl API获取时区数据,而不必自己发货。

Fortunately, there are libraries that can be used to work with time zones. Though they still cannot make the Date object behave any differently, they typically implement the standard Olson/IANA timezone database and provide functions for using it in JavaScript. Some have overhead if you are running in a web browser, as the database can get a bit large if you want the whole thing. Fortunately, many of these libraries allow you to selectively choose which zones you want to support, making the data size much more palatable. Also some use modern features to get time zone data from the Intl API instead of having to ship it themselves.

我知道有几个这样的库:

There are several libraries for this that I am aware of:

  • Luxon (successor of Moment.js)
  • moment-timezone (extension for Moment.js)
  • js-joda (js port of Java ThreeTen)
  • date-fns-tz / date-fns-timezone (extensions for date-fns)
  • BigEasy/TimeZone
  • WallTime-js (discontinued)
  • TimeZoneJS
  • tz.js

Luxon可能是所有现代用途中最安全的选择,并且是最轻的权重,因为它使用 Intl API作为其时区数据。

Luxon is probably the safest bet for all modern usage, and is the lightest weight as it uses the Intl API for its timezone data.

Moment-timezone是一个扩展名 moment.js ,并提供自己的时区数据。

Moment-timezone is an extension to moment.js, and brings its own time zone data.

js-joda是Joda-Time API的JavaScript实现(来自Java),并通过单独的模块包含时区支持。

js-joda is a JavaScript implementation of the Joda-Time API (from Java), and includes time zone support through a separate module.

date-fns-tz是 date-fns 的扩展名2。 X。 date-fns-timezone是 date-fns 1.x的扩展名。

date-fns-tz is an extension for date-fns 2.x. date-fns-timezone is an extension for date-fns 1.x.

BigEasy / TimeZone似乎也在正确的轨道上。

BigEasy/TimeZone also appears to be on the right track.

WallTime-js 已达到使用寿命结束,并且所有者正在迁移到时刻时区。

WallTime-js has reached end-of-life, and the owners are migrating to moment-timezone.

TimeZoneJS已经存在时间最长,但已知有一些长期存在的错误,特别是在夏令时转换时。希望这些将在未来的某个时刻得到修复。

TimeZoneJS has been around the longest, but is known to have some long-standing bugs, especially near daylight saving time transitions. Hopefully these will be fixed at some point in the future.

tz.js也已存在一段时间了,但记录不是很好,恕我直言。

tz.js has also been around for some time, but isn't very well documented, IMHO.

您应该评估这些库以查看哪些库可以满足您的需求。如果不确定,请使用时刻/时刻 - 时区。

You should evaluate these libraries to see which will meet your needs. If unsure, go with moment/moment-timezone.

如果可以限制您现在可以在没有任何特殊库的情况下执行以下操作:

If you can limit your usage to modern web browsers, you can now do the following without any special libraries:

new Date().toLocaleString("en-US", {timeZone: "America/New_York"})

这不是一个全面的解决方案,但它适用于许多只需要输出转换的场景(从UTC或本地时间到特定时区,但不是另一个方向)。这是ECMAScript国际化API(ECMA-402)的一部分。有关详细信息,请参阅此帖子此兼容性表可跟踪支持的版本。这是上面提到的某些库现在在内部使用的 Intl API。

This isn't a comprehensive solution, but it works for many scenarios that require only output conversion (from UTC or local time to a specific time zone, but not the other direction). This is part of the ECMAScript Internationalization API (ECMA-402). See this post for more details. This compatibility table tracks which versions are supported. This is the Intl API mentioned above that certain libraries are using internally now.

TC39 Temporal Proposal 旨在提供一套新的套装用于在JavaScript语言本身中处理日期和时间的标准对象。这将包括对时区感知对象的支持。

The TC39 Temporal Proposal aims to provide a new set of standard objects for working with dates and times in the JavaScript language itself. This will include support for a time zone aware object.

这篇关于如何将JavaScript日期初始化为特定时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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