不同区域和时区的 JavaScript 日期对象
我需要编写一个 Web 应用程序来显示不同地区的人们的事件。我几乎完成了它,但是日期有两个问题:
- 使用日期javascript对象,日期取决于用户计算机设置,
- 如果在不同时区的地方有一个事件尊重用户当前位置,它是不可靠的,我必须打印它在
()
内。是否可以在 javascript 中构建具有给定时区和日光设置的日期对象?
我还找到了一些解决方法,例如 jsdate 和日期网络服务,但它们没有克服具有正确时区和日光设置的 javascript 对象的问题(用于日期操作,例如添加天数等)。
I need to write a web application that show events of people in different locale. I almost finished it, but there're 2 problems with date:
- using date javascript object, the date depends on user computer settings and it's not reliable
- if there's an event in a place with dfferent timezone respect user current position, i have to print it inside
()
. Is it possible in javascript to build a date object with a given timezone and daylight settings?
I also find some workaround, such as jsdate and date webservices, but they don't overcome the problem of having a javascript object with the correct timezone and daylight settings (for date operation such as adding days and so on).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有几点需要记住。
以 UTC 时间存储所有事件日期时间
是的,无法解决这个问题。
找出
系统中所有用户的所有时区... ...。您可以使用以下检测脚本: http://site.pageloom.com/automatic -时区检测-with-javascript。它会给您一个时区键,例如“America/Phoenix”。
在您的情况下,您需要将时区与事件一起存储,因为用户可能会切换时区 - 但事件将始终发生在特定的时区。 (argh)
选择您的显示机制
如果您想使用 Javascript 本地化您的事件日期,也有一个漂亮的库(可以使用前面脚本提供的键)。这里:https://github.com/mde/timezone-js。
使用该库,您可以执行以下操作:
或者
例如 UTC_TIMESTAMP 可以是
1193855400000
。America/New_York
是您在事件发生时检测到的时区。从中获得的 dt 对象将表现为普通的 JavaScript Date 对象。但会自动“更正”您指定的时区(包括夏令时)。
如果您愿意,您可以在提供页面之前在后端进行所有更正。由于我不知道您在那里使用什么编程语言,因此我无法立即给您任何提示。但基本上它遵循相同的逻辑,如果您知道时区和 UTC 日期时间 ->您可以本地化日期时间。所有编程语言都有相应的库。
A couple of things to keep in mind.
Store all event datetimes in UTC time
Yes, there is no getting around this.
Find out all the timezones...
...of all the users in the system. You can use the following detection script: http://site.pageloom.com/automatic-timezone-detection-with-javascript. It will hand you a timezone key such as for example "America/Phoenix".
In your case you need to store the timezone together with the event, since a user may switch timezone - but the event will always have happened in a specific one. (argh)
Choose your display mechanism
If you want to localize your event dates with Javascript, there is a nifty library for that too (which can use the keys supplied with the previous script). Here: https://github.com/mde/timezone-js.
with that library you can for example do this:
or
where UTC_TIMESTAMP for example could be
1193855400000
. AndAmerica/New_York
is the timezone you have detected when the event took place.The
dt
object that you get from this will behave as a normal JavaScriptDate
object. But will automatically "correct" itself to the timezone you have specified (including DST).If you want to, you can do all the corrections in the backend - before you serve the page. Since I don't know what programming language you are using there, I cannot give you any immediate tips. But basically it follows the same logic, if you know the timezone, and the UTC datetime -> you can localize the datetime. All programming languages have libraries for that.
您错过了 Date 对象的要点。它代表了一个特定的时间点。正如我所说,全世界都是1308150623182。仅当您想要向用户显示时间时,时区才会发挥作用。像“添加一天”这样的操作根本不涉及时区。
You're missing the point of a Date object. It represents a particular point in time. As I speak, it is 1308150623182 all over the world. Timezone only comes into play when you want to display the time to the user. An operation like "adding a day" does not involve the time zone at all.
一种可能性是对所有内容都使用 UTC 日期和时间。这样,就没有什么可以转换的了。
另一种方法是让您的服务器提供时间和日期。然后,您不必依赖用户来正确设置它,也不必担心用户的时区在哪里。
One possibility might be to use UTC date and time for everything. That way, there is nothing to convert.
Another is to have your server provide the time and date. Then you don't have to depend on the user to have it set correctly, and you don't have to worry about where your user's timezone is.
使用
getUTCDate()
、getUTCHours()
、... 代替getDate()
、getHours()
、 ...getTimetoneOffset()
也可能很有用。Use
getUTCDate()
,getUTCHours()
, ... instead ofgetDate()
,getHours()
,...getTimetoneOffset()
could be useful, too.