不同语言环境和时区的 Javascript 日期对象 [英] Javascript date object in different locale and timezone

查看:37
本文介绍了不同语言环境和时区的 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 对象,日期取决于用户计算机设置,并不可靠
  • 如果在具有不同时区的地方发生事件尊重用户当前位置,我必须在 () 内打印它.是否可以在 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 和 date webservices,但它们并没有解决具有正确时区和日光设置的 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 时间存储所有事件日期时间

是的,没有办法解决这个问题.

Yes, there is no getting around this.

找出所有时区...

...系统中的所有用户.您可以使用以下检测脚本: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".

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

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 日期时间 -> 您可以本地化日期时间.所有编程语言都有相应的库.

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天全站免登陆