Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

javascript - 如何将JavaScript日期初始化为特定时区(How to initialize a JavaScript Date to a particular time zone)

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.

(但是,我不知道如何在Date对象中设置时区。)

For example, I have Feb 28 2013 7:00 PM ET, then I can

(例如,我有Feb 28 2013 7:00 PM ET,那么我可以)

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

As far as I know, I can either set the UTC time or local time.

(据我所知,我可以设置UTC时间或本地时间。)

But, how do I set time in another timezone?

(但是,如何设置其他时区的时间?)

I tried to use the add/subtract the offset from UTC but I don't know how to counter daylight savings.

(我尝试使用加/减UTC的偏移量,但是我不知道如何应对夏令时。)

Am not sure if I am heading the right direction.

(不知道我是否朝着正确的方向前进。)

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

(如何在javascript中将时间从其他时区转换为本地时间?)

  ask by pavanred translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...