Javascript日期对象在不同的​​区域和时区 [英] Javascript date object in different locale and timezone

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

问题描述

我需要编写一个Web应用程序来显示不同语言环境中的人的事件。我几乎完成了,但日期有两个问题:

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:


  • 使用日期javascript对象,日期取决于用户计算机设置,它是不可靠

  • 如果在某个地方有一个事件与dfferent时区尊重用户当前位置,我必须打印在()。可以在javascript中使用给定的时区和日光设置构建日期对象吗?

  • 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?

我还找到一些解决方法,如jsdate和日期的web服务,但是他们并没有克服使用正确的时区和日光设置的JavaScript对象(日期操作,如添加日期等)的问题。

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).

推荐答案

要记住的几件事。

将所有事件数据时间存储在UTC时间

是的,没有任何解决。

查找所有的时区

...系统中的所有用户。您可以使用以下检测脚本: http://site.pageloom.com/automatic -timezone-detection-with-javascript 。它会给你一个时区键,例如America / Phoenix。

...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".

在你的情况下,你需要将时区与事件一起存储,因为用户可能切换时区 - 但事件将始终发生在特定的一个。 (argh)

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)

选择您的展示机制

如果要本地化您的事件日期与Javascript,还有一个漂亮的库(可以使用以前的脚本提供的键)。这里: https://github.com/mde/timezone-js

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:

var dt = new timezoneJS.Date(UTC_TIMESTAMP, 'America/New_York');

var dt = new timezoneJS.Date(2006, 9, 29, 1, 59, 'America/Los_Angeles');

其中UTC_TIMESTAMP可以是 1193855400000 。而 America / New_York 是您在事件发生时检测到的时区。

where UTC_TIMESTAMP for example could be 1193855400000. And America/New_Yorkis the timezone you have detected when the event took place.

您从此获取的 dt对象将作为正常的JavaScript Date 对象。但是会自动将其自动修正到您指定的时区(包括DST)。

The dt object that you get from this will behave as a normal JavaScript Date object. But will automatically "correct" itself to the timezone you have specified (including DST).

如果你想,可以在后台执行所有更正 - 在你之前服务页面。既然我不知道你在那里使用什么编程语言,我不能给你任何即时提示。但基本上它遵循相同的逻辑,如果你知道时区,UTC datetime - >可以本地化datetime。所有编程语言都有这样的库。

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.

这篇关于Javascript日期对象在不同的​​区域和时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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