問(wèn)題描述
我需要編寫一個(gè) Web 應(yīng)用程序來(lái)顯示不同地區(qū)的人們的事件.我差不多完成了,但是日期有兩個(gè)問(wèn)題:
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 對(duì)象,日期取決于用戶計(jì)算機(jī)設(shè)置,并不可靠
- 如果在具有不同時(shí)區(qū)的地方發(fā)生事件尊重用戶當(dāng)前位置,我必須在
()
內(nèi)打印它.是否可以在 javascript 中構(gòu)建具有給定時(shí)區(qū)和日光設(shè)置的日期對(duì)象?
- 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,但它們并沒(méi)有解決具有正確時(shí)區(qū)和日光設(shè)置的 javascript 對(duì)象的問(wèn)題(用于日期操作,例如添加天數(shù)等).
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).
推薦答案
有幾點(diǎn)要記住.
以 UTC 時(shí)間存儲(chǔ)所有事件日期時(shí)間
是的,沒(méi)有辦法解決這個(gè)問(wèn)題.
Yes, there is no getting around this.
找出所有時(shí)區(qū)...
...系統(tǒng)中的所有用戶.您可以使用以下檢測(cè)腳本:http://site.pageloom.com/automatic-timezone-detection-with-javascript.它會(huì)給你一個(gè)時(shí)區(qū)鍵,例如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".
在您的情況下,您需要將時(shí)區(qū)與事件一起存儲(chǔ),因?yàn)橛脩艨赡軙?huì)切換時(shí)區(qū) - 但事件將始終在特定時(shí)區(qū)發(fā)生.(啊)
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)
選擇您的顯示機(jī)制
如果您想使用 Javascript 本地化您的活動(dòng)日期,也有一個(gè)漂亮的庫(kù)(可以使用上一個(gè)腳本提供的鍵).這里: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.
使用該庫(kù),您可以執(zhí)行以下操作:
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
是您在事件發(fā)生時(shí)檢測(cè)到的時(shí)區(qū).
where UTC_TIMESTAMP for example could be 1193855400000
. And America/New_York
is the timezone you have detected when the event took place.
您從中獲得的 dt
對(duì)象將表現(xiàn)為普通的 JavaScript Date
對(duì)象.但會(huì)自動(dòng)更正"到您指定的時(shí)區(qū)(包括 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).
如果您愿意,您可以在提供頁(yè)面之前在后端進(jìn)行所有更正.因?yàn)槲也恢滥阍谀抢锸褂檬裁淳幊陶Z(yǔ)言,所以我不能給你任何直接的提示.但基本上它遵循相同的邏輯,如果您知道時(shí)區(qū)和 UTC 日期時(shí)間 -> 您可以本地化日期時(shí)間.所有編程語(yǔ)言都有相應(yīng)的庫(kù).
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.
這篇關(guān)于不同語(yǔ)言環(huán)境和時(shí)區(qū)的 Javascript 日期對(duì)象的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!