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

    1. <small id='Av67P'></small><noframes id='Av67P'>

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

    2. <tfoot id='Av67P'></tfoot>

      不同语言环境和时区的 Javascript 日期对象

      时间:2023-06-14

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

          <bdo id='41jwh'></bdo><ul id='41jwh'></ul>

          <small id='41jwh'></small><noframes id='41jwh'>

          <tfoot id='41jwh'></tfoot>
            <tbody id='41jwh'></tbody>
          • <legend id='41jwh'><style id='41jwh'><dir id='41jwh'><q id='41jwh'></q></dir></style></legend>
                本文介绍了不同语言环境和时区的 Javascript 日期对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我需要编写一个 Web 应用程序来显示不同地区的人们的事件.我差不多完成了,但是日期有两个问题:

                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 对象,日期取决于用户计算机设置,并不可靠
                • 如果在具有不同时区的地方发生事件尊重用户当前位置,我必须在 () 内打印它.是否可以在 javascript 中构建具有给定时区和日光设置的日期对象?
                • 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,但它们并没有解决具有正确时区和日光设置的 javascript 对象的问题(用于日期操作,例如添加天数等).

                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).

                推荐答案

                有几点要记住.

                以 UTC 时间存储所有事件日期时间

                是的,没有办法解决这个问题.

                Yes, there is no getting around this.

                找出所有时区...

                ...系统中的所有用户.您可以使用以下检测脚本:http://site.pageloom.com/automatic-timezone-detection-with-javascript.它会给你一个时区键,例如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".

                在您的情况下,您需要将时区与事件一起存储,因为用户可能会切换时区 - 但事件将始终在特定时区发生.(啊)

                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)

                选择您的显示机制

                如果您想使用 Javascript 本地化您的活动日期,也有一个漂亮的库(可以使用上一个脚本提供的键).这里: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.

                使用该库,您可以执行以下操作:

                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是您在事件发生时检测到的时区.

                where UTC_TIMESTAMP for example could be 1193855400000. And America/New_Yorkis the timezone you have detected when the event took place.

                您从中获得的 dt 对象将表现为普通的 JavaScript Date 对象.但会自动更正"到您指定的时区(包括 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).

                如果您愿意,您可以在提供页面之前在后端进行所有更正.因为我不知道你在那里使用什么编程语言,所以我不能给你任何直接的提示.但基本上它遵循相同的逻辑,如果您知道时区和 UTC 日期时间 -> 您可以本地化日期时间.所有编程语言都有相应的库.

                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.

                这篇关于不同语言环境和时区的 Javascript 日期对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:使用 moment.js 将 Utc 日期更改为本地日期 下一篇:消除 Javascript 夏令时差距,一种跨浏览器的解决方案

                相关文章

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

                  <legend id='am10V'><style id='am10V'><dir id='am10V'><q id='am10V'></q></dir></style></legend>

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

                  1. <tfoot id='am10V'></tfoot>

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