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.
(JavaScript的Date
对象在内部跟踪UTC时间,但通常在运行它的计算机的本地时间接受输入和输出。)
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 .(它可以解析和输出UTC或本地日期,但不能直接与其他时区一起使用 。)
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.
(确切地说, Date
对象的内部表示形式是单个数字,表示自1970-01-01 00:00:00 UTC
以来经过的毫秒数,而与leap秒1970-01-01 00:00:00 UTC
。)
There is no time zone or string format stored in the Date object itself.(Date对象本身没有存储任何时区或字符串格式。)
When various functions of the Date
object are used, the computer's local time zone is applied to the internal representation.(当使用Date
对象的各种功能时,计算机的本地时区将应用于内部表示。)
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.(具体细节因功能而异,有些具体取决于实现。)
Libraries(图书馆)
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.(尽管他们仍然无法使Date
对象的行为有所不同,但它们通常实现标准的Olson / IANA时区数据库并提供在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.(如果您在Web浏览器中运行,则有些会产生开销,因为如果您需要整个数据库,那么数据库可能会有点大。)
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.(还有一些人使用现代功能从Intl
API获取时区数据,而不必自己发送。)
There are several libraries for this that I am aware of:
(我知道有几个与此相关的库:)
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.
(Luxon可能是所有现代用途中最安全的选择,并且重量轻,因为它使用Intl
API为其时区数据。)
Moment-timezone is an extension to moment.js , and brings its own time zone data.
(Moment-timezone是moment.js的扩展,并带有自己的时区数据。)
js-joda is a JavaScript implementation of the Joda-Time API (from Java), and includes time zone support through a separate module.
(js-joda是Joda-Time API(来自Java)的JavaScript实现,并通过单独的模块提供时区支持。)
date-fns-tz is an extension for date-fns 2.x.
(date-fns-tz是date-fns 2.x的扩展。)
date-fns-timezone is an extension for date-fns 1.x.(date-fns-timezone是date-fns 1.x的扩展。)
BigEasy/TimeZone also appears to be on the right track.
(BigEasy / TimeZone也似乎在正确的轨道上。)
WallTime-js has reached end-of-life , and the owners are migrating to moment-timezone.
(WallTime-js 已达到使用寿命 ,并且所有者正在迁移到时区。)
TimeZoneJS has been around the longest, but is known to have some long-standing bugs, especially near daylight saving time transitions.
(TimeZoneJS的使用时间最长,但已知存在一些长期存在的错误,尤其是在夏令时过渡附近。)
Hopefully these will be fixed at some point in the future.(希望这些将在将来的某个时间解决。)
tz.js has also been around for some time, but isn't very well documented, IMHO.
(tz.js也已经存在了一段时间,但是恕我直言,并没有得到很好的记录。)
You should evaluate these libraries to see which will meet your needs.
(您应该评估这些库,看看哪些将满足您的需求。)
If unsure, go with moment/moment-timezone.(如果不确定,请使用力矩/力矩时区。)
Native Support in Modern Environments(现代环境中的本地支持)
If you can limit your usage to modern environments, you can now do the following without any special libraries:
(如果可以将使用限制在现代环境中,则现在可以执行以下操作而无需任何特殊的库:)
new Date().toLocaleString("en-US", {timeZone: "America/New_York"})
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).
(这不是一个全面的解决方案,但是它适用于仅需要输出转换(从UTC或本地时间到特定时区,而不是其他方向)的许多情况。)
This is part of the ECMAScript Internationalization API (ECMA-402).(这是ECMAScript国际化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.(这是某些库现在正在内部使用的上述Intl
API。)
(To be clear, this doesn't initialize a Date
object, but can be used to apply a time zone when producing a locale-specific string representation.)
((请注意,这不会初始化Date
对象,但是在生成特定于语言环境的字符串表示形式时,可用于应用时区。))
Future Proposals(未来提案)
The TC39 Temporal Proposal aims to provide a new set of standard objects for working with dat