我的单页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:
有没有更简单或更好的解决方案?
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中使用时区和夏令时
发布评论