在Javascript中使用时区和夏令时 [英] Working with timezones and daylight savings time in Javascript

查看:533
本文介绍了在Javascript中使用时区和夏令时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的单页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:


  1. 服务器将日期作为已经格式化在正确时区的字符串,并且没有日期解析或操作完成在客户端上这使得在客户端上做某些事情变得困难(如果不是不可能)。

  2. 使用一个库,如 https://github.com/mde/timezone-js ,其中包含整个Olson TZ数据库到Javascript。这意味着更长的加载时间,更多的内存使用率等。

  3. 发送一个timezoneOffsetMillis值,并将每个日期发送给客户端。这样会产生凌乱的JSON数据和非最佳的REST接口。

  1. 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.
  2. 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.
  3. 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中使用时区和夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆