在特定时区中创建日期对象 [英] Creating a Date object in a specific time zone

查看:80
本文介绍了在特定时区中创建日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Java语言 Date 对象传递到Angular模型中,以便它可以在日期/时间选择器中向用户显示日期和时间。

I need to pass in a Javascript Date object into a Angular model so that it can display the date and time to the user in a date/time picker.

数据以以下格式存储在数据库中:

The data is stored in the database in the following format:

date: Sat, 23 Sep 2017 21:00:00 CDT -05:00,

此数据通过到Angular指令中,我需要在其中创建一个表示它的 Date 对象。问题是,每当实例化 new Date()时,它总是使用用户计算机的时区。例如,如果用户的计算机位于旧金山,则上述日期对象将显示星期六9月23日19:00:00 GMT-0700(PDT)

This data is passed into an Angular directive, where I need to create a Date object representing it. The issue is that whenever a new Date() is instantiated, it always uses the timezone of the users machine. For example, the above date object would display Sat Sep 23 2017 19:00:00 GMT-0700 (PDT) if the users machine was in San Francisco.

我试图以多种方式操纵此日期,包括:

I have tried to manipulate this date in a variety of ways, including:


  1. 使用时刻.js进行时区操作,然后在结果时刻调用 .toDate()

  2. 使用 date.getTime()和<$ c $的变体c> date.getTimezoneOffset()来计算自所选时区的纪元以来的合适毫秒数。

  3. 将字符串的所有时区信息完全剥离。这会导致 Date 对象错误地解释时间。

  1. Using moment.js for timezone manipulation and then calling .toDate() on the resulting moment. This discards all timezone information and brings us back to square one.
  2. Using variations of date.getTime() and date.getTimezoneOffset() to calculate the appropriate the milliseconds since epoch for the selected timezone. This also does not work as expected.
  3. Stripping the String entirely of it's timezone information. This results in the Date object interpreting the time incorrectly.

我该如何创建一个Javascript Date 对象,该对象正确地表示传入的字符串并尊重时区信息?

How can I create a Javascript Date object that properly represents the String passed in and respects the timezone information?

推荐答案


如何创建一个Javascript Date对象,以正确表示传入的字符串并尊重时区信息?

How can I create a Javascript Date object that properly represents the String passed in and respects the timezone information?

尊重时区信息是什么意思?如果您的意思是继续使用-05:00的偏移量,那么这很简单:解析字符串,存储偏移量,并在从日期开始创建时间戳时使用它。

What do you mean by "respects the timezone information"? If you mean "continues to use an offset of -05:00", then that is fairly straight forward: parse the string, store the offset and use it whenever creating a timestamp from the date.

但是,如果您还想观察夏令时的变化,您将面临更大的挑战。假设 CDT是中央夏令时,那么使用它的某些地方在赤道以北(例如美国的地方),而某些地方在赤道以南,例如

However, if you also want to observe daylight saving changes, you have a much bigger challenge. Assuming "CDT" is "Central Daylight (Saving) Time", then some places that use it are north of the equator (e.g. places in USA) and some places are south of the equator, e.g.

Place              CDT starts     CDT ends
Tabasco, Mexico:    2 Apr 2017    29 Oct 2017
Chicago, USA:      12 Mar 2017     5 Nov 2017
Easter Is, Chile:  12 Aug 2017    12 May 2018 <-- Southern hemisphere

您可以看到,仅仅是因为某个地方使用CDT并不意味着您可以确定它何时从CDT更改为CST。

So as you can see, just because a place uses CDT doesn't mean you can determine when it changes from CDT to CST.

以上是现在很常见的原因以使用IANA时区数据库位置标识符(请参见 Wikipedia )。它允许用户根据位置而不是特定时区来指定特定的偏移量和规则。

The above is the reason why it is now common to use the IANA timezone database location identifiers (see Wikipedia). It allows a user to specify particular offsets and rules based on a location rather than a specific timezone.

因此,除非您可以将 CDT -05:00解析为,美国/芝加哥或智利/复活节岛,您可以为所有时间戳保留 CDT -05:00,使用UTC或使用UTC并根据主机系统转换为本地。

So unless you can resolve "CDT -05:00" to, say, "America/Chicago" or "Chile/EasterIsland", you can either keep "CDT -05:00" for all timestamps, use UTC, or use UTC and convert to "local" based on the host system.

这篇关于在特定时区中创建日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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