<small id='IdgZl'></small><noframes id='IdgZl'>

<tfoot id='IdgZl'></tfoot>
<legend id='IdgZl'><style id='IdgZl'><dir id='IdgZl'><q id='IdgZl'></q></dir></style></legend>

    1. <i id='IdgZl'><tr id='IdgZl'><dt id='IdgZl'><q id='IdgZl'><span id='IdgZl'><b id='IdgZl'><form id='IdgZl'><ins id='IdgZl'></ins><ul id='IdgZl'></ul><sub id='IdgZl'></sub></form><legend id='IdgZl'></legend><bdo id='IdgZl'><pre id='IdgZl'><center id='IdgZl'></center></pre></bdo></b><th id='IdgZl'></th></span></q></dt></tr></i><div id='IdgZl'><tfoot id='IdgZl'></tfoot><dl id='IdgZl'><fieldset id='IdgZl'></fieldset></dl></div>
        <bdo id='IdgZl'></bdo><ul id='IdgZl'></ul>

      在 Javascript 中使用时区和夏令时

      时间:2023-10-11
      <tfoot id='OVSPj'></tfoot>

          <small id='OVSPj'></small><noframes id='OVSPj'>

                <tbody id='OVSPj'></tbody>
              <legend id='OVSPj'><style id='OVSPj'><dir id='OVSPj'><q id='OVSPj'></q></dir></style></legend>
                <bdo id='OVSPj'></bdo><ul id='OVSPj'></ul>
                <i id='OVSPj'><tr id='OVSPj'><dt id='OVSPj'><q id='OVSPj'><span id='OVSPj'><b id='OVSPj'><form id='OVSPj'><ins id='OVSPj'></ins><ul id='OVSPj'></ul><sub id='OVSPj'></sub></form><legend id='OVSPj'></legend><bdo id='OVSPj'><pre id='OVSPj'><center id='OVSPj'></center></pre></bdo></b><th id='OVSPj'></th></span></q></dt></tr></i><div id='OVSPj'><tfoot id='OVSPj'></tfoot><dl id='OVSPj'><fieldset id='OVSPj'></fieldset></dl></div>
                本文介绍了在 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 中使用时区和夏令时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:为什么 JavaScript Date.getTimezoneOffset() 会考虑“-05 下一篇:在 MongoDB 中存储日期而不考虑时区

                相关文章

                最新文章

              • <small id='TkOZL'></small><noframes id='TkOZL'>

                <tfoot id='TkOZL'></tfoot>

              • <i id='TkOZL'><tr id='TkOZL'><dt id='TkOZL'><q id='TkOZL'><span id='TkOZL'><b id='TkOZL'><form id='TkOZL'><ins id='TkOZL'></ins><ul id='TkOZL'></ul><sub id='TkOZL'></sub></form><legend id='TkOZL'></legend><bdo id='TkOZL'><pre id='TkOZL'><center id='TkOZL'></center></pre></bdo></b><th id='TkOZL'></th></span></q></dt></tr></i><div id='TkOZL'><tfoot id='TkOZL'></tfoot><dl id='TkOZL'><fieldset id='TkOZL'></fieldset></dl></div>

                    • <bdo id='TkOZL'></bdo><ul id='TkOZL'></ul>

                  1. <legend id='TkOZL'><style id='TkOZL'><dir id='TkOZL'><q id='TkOZL'></q></dir></style></legend>