問題描述
我的單頁 javascript 應用程序通過 REST 調用以 JSON 格式檢索數據.日期使用標準 ISO8601 格式的 UTC 時區格式化,例如 2011-02-04T19:31:09Z
.
My single-page javascript app retrieves data in JSON format via REST calls. Dates come formatted using the UTC timezone in standard ISO8601 format, such as 2011-02-04T19:31:09Z
.
注冊服務時,用戶從下拉列表中選擇他們的時區.此時區可能與用戶瀏覽器的時區不同.javascript 應用程序始終知道用戶選擇的時區.
When signing up for the service, users select their timezone from a drop down list. This timezone could be different than the user's browser's timezone. The javascript app knows what the user's selected timezone is at all times.
我知道如何將 UTC 字符串轉換為日期.我了解 Javascript 僅代表本地時區的日期.
I know how to convert the UTC string into a date. I understand that Javascript only represents dates in the local timezone.
但我無法弄清楚如何顯示針對用戶本地時區以外的時區格式化的日期.它必須考慮所有日期的 DST.在內部,我想將所有日期作為 UTC 處理,并且只在顯示時間轉換為另一個時區中日期的字符串表示形式.我需要在用戶個人資料中選擇的時區顯示日期,而不是他們瀏覽器的時區.
But I'm having troubles figuring out how to display a date formatted for a timezone OTHER than the user's local timezone. It must account for DST on all dates. Internally, I want to deal with all dates as UTC and only convert to string representation of a date in another timezone at display time. I need to display the dates in the timezone selected in the user's profile, not their browser's timezone.
我已經嘗試使用服務器發送用戶瀏覽器時區和用戶配置文件時區之間的時區偏移差異(以毫秒為單位).但我發現我不能只發送一個偏移量值,而是需要為每個日期發送一個偏移量以說明 DST 的變化.
I've experimented with the server sending the timezone offset difference in milliseconds between the user's browser's timezone and the user's profile timezone. But I've found I can't just send one offset value, but need to send an offset for every date to account for variations in DST.
關于如何顯示不同時區格式的日期的任何建議或示例代碼?到目前為止我發現的選項:
Any suggestions or sample code on how to display dates formatted in various timezones? The options I've found so far:
- 服務器將日期作為已在正確時區格式化的字符串發送,并且沒有在客戶端上進行日期解析或操作.這使得在客戶端上做某些事情即使不是不可能也很困難.
- 使用庫,例如?? https://github.com/mde/timezone-js,其中包括整個 Olson TZ 數據庫到 Javascript.這意味著更長的加載時間更多的內存使用等.
- 發送一個 timezoneOffsetMillis 值以及每個發送給客戶端的日期.這會導致混亂的 JSON 數據和非最佳 REST 接口.
- Server sends dates as strings already formatted in the right timezone and no date parsing or manipulation is done on the client. This makes doing certain things on the client difficult if not impossible.
- Use a library such as https://github.com/mde/timezone-js, which includes the entire Olson TZ database into Javascript. This means longer load times more memory usage, etc.
- Send a timezoneOffsetMillis value with every date sent to the client. This results in messy JSON data and non-optimal REST interfaces.
有沒有更簡單或更好的解決方案?
Are there any simpler or better solutions?
推薦答案
2 是個壞主意,因為正如您所指出的,它會增加加載時間.如果我是你,我會做 1 和 3 的組合.我不同意這會導致 JSON 數據混亂或 REST 接口非最優.
2 is a bad idea since, as you pointed out, it increases load times. If I were you I would do a combination of 1 and 3. I don't agree that this makes the JSON data messy or the REST interface non-optimal.
這是一個經典的權衡,在協議中接受更多的復雜性以簡化客戶端代碼.
This is a classic tradeoff where to accept a bit more complexity in the protocol in order to simplify the client-side code.
這篇關于在 Javascript 中使用時區和夏令時的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!