javascript 不同语言环境和时区的Javascript日期对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6356839/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript date object in different locale and timezone
提问by Ruben Rizzi
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:
我需要编写一个 Web 应用程序来显示不同语言环境中的人们的事件。我快完成了,但日期有两个问题:
- 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?
- 使用日期 javascript 对象,日期取决于用户计算机设置并且不可靠
- 如果在具有不同时区尊重用户当前位置的地方发生事件,我必须将其打印在里面
()
。是否可以在 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).
我还找到了一些解决方法,例如 jsdate 和 date webservices,但它们没有克服具有正确时区和日光设置的 javascript 对象的问题(用于日期操作,例如添加天数等)。
回答by Jon Nylander
A couple of things to keep in mind.
有几件事要记住。
Store all event datetimes in UTC time
以 UTC 时间存储所有事件日期时间
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".
...系统中的所有用户。您可以使用以下检测脚本:http: //site.pageloom.com/automatic-timezone-detection-with-javascript。它会给你一个时区键,例如“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.
如果你想用 Javascript 本地化你的事件日期,也有一个漂亮的库(它可以使用上一个脚本提供的键)。在这里: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');
or
或者
var dt = new timezoneJS.Date(2006, 9, 29, 1, 59, 'America/Los_Angeles');
where UTC_TIMESTAMP for example could be 1193855400000
. And America/New_York
is the timezone you have detected when the event took place.
例如,其中 UTC_TIMESTAMP 可能是1193855400000
. 并且America/New_York
是您在事件发生时检测到的时区。
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).
dt
您从中获得的对象将作为一个普通的 JavaScriptDate
对象运行。但会自动“更正”到您指定的时区(包括 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.
如果您愿意,您可以在后端进行所有更正 - 在您提供页面之前。由于我不知道您在那里使用什么编程语言,因此我无法立即为您提供任何提示。但基本上它遵循相同的逻辑,如果您知道时区和 UTC 日期时间 -> 您可以本地化日期时间。所有编程语言都有相应的库。
回答by Malvolio
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 displaythe time to the user. An operation like "adding a day" does not involve the time zone at all.
您错过了 Date 对象的要点。它代表一个特定的时间点。在我说话的时候,全世界都是 1308150623182。时区仅在您想向用户显示时间时才起作用。像“添加一天”这样的操作根本不涉及时区。
回答by James Alarie
One possibility might be to use UTC date and time for everything. That way, there is nothing to convert.
一种可能性可能是对所有内容使用 UTC 日期和时间。这样,就没有什么可以转换的了。
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.
另一种方法是让您的服务器提供时间和日期。这样您就不必依赖用户来正确设置它,也不必担心用户的时区在哪里。
回答by Meeple
Use getUTCDate()
, getUTCHours()
, ... instead of getDate()
, getHours()
,...
getTimetoneOffset()
could be useful, too.
使用getUTCDate()
, getUTCHours()
, ... 而不是getDate()
, getHours()
,...
getTimetoneOffset()
也很有用。