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

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

        在特定时区格式化日期

        时间:2023-10-12

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

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

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

                <bdo id='yrWGK'></bdo><ul id='yrWGK'></ul>

                  <tfoot id='yrWGK'></tfoot>
                    <tbody id='yrWGK'></tbody>
                  本文介绍了在特定时区格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我正在使用 Moment.js 在我的网络应用程序中解析和格式化日期.作为 JSON 对象的一部分,我的后端服务器以 UTC 纪元(Unix 偏移量)的毫秒数发送日期.

                  I'm using Moment.js to parse and format dates in my web app. As part of a JSON object, my backend server sends dates as a number of milliseconds from the UTC epoch (Unix offset).

                  在特定时区解析日期很容易——只需在解析前将 RFC 822 时区标识符附加到字符串的末尾即可:

                  Parsing dates in a specific timezone is easy -- just append the RFC 822 timezone identifier to the end of the string before parsing:

                  // response varies according to your timezone
                  const m1 = moment('3/11/2012 13:00').utc().format("MM/DD HH:mm")
                  
                  // problem solved, always "03/11 17:00"
                  const m2 = moment('3/11/2012 13:00 -0400').utc().format("MM/DD HH:mm")
                  
                  console.log({ m1, m2 })

                  <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js"></script>

                  但是如何在特定时区格式化日期?

                  无论浏览器的当前时间如何,我都想要一致的结果,但我不想以 UTC 显示日期.

                  I want consistent results regardless of the browser's current time, but I don't want to display dates in UTC.

                  推荐答案

                  正如Manto's answer中指出的,.utcOffset() 是 Moment 的首选方法2.9.0.此函数使用与 UTC 的实际偏移量,而不是反向偏移量(例如,DST 期间的纽约为 -240).像+0400"这样的偏移字符串的工作方式和以前一样:

                  As pointed out in Manto's answer, .utcOffset() is the preferred method as of Moment 2.9.0. This function uses the real offset from UTC, not the reverse offset (e.g., -240 for New York during DST). Offset strings like "+0400" work the same as before:

                  // always "2013-05-23 00:55"
                  moment(1369266934311).utcOffset(60).format('YYYY-MM-DD HH:mm')
                  moment(1369266934311).utcOffset('+0100').format('YYYY-MM-DD HH:mm')
                  

                  较旧的 .zone() 作为二传手在 Moment.js 2.9.0 中被弃用.它接受包含时区标识符的字符串(例如,-4 小时的-0400"或-04:00")或代表 UTC 分钟的数字(例如,DST 期间纽约的 240).

                  The older .zone() as a setter was deprecated in Moment.js 2.9.0. It accepted a string containing a timezone identifier (e.g., "-0400" or "-04:00" for -4 hours) or a number representing minutes behind UTC (e.g., 240 for New York during DST).

                  // always "2013-05-23 00:55"
                  moment(1369266934311).zone(-60).format('YYYY-MM-DD HH:mm')
                  moment(1369266934311).zone('+0100').format('YYYY-MM-DD HH:mm')
                  

                  <小时>

                  要使用命名时区而不是数字偏移量,请包含 Moment Timezone 并使用 .tz() 改为:

                  // determines the correct offset for America/Phoenix at the given moment
                  // always "2013-05-22 16:55"
                  moment(1369266934311).tz('America/Phoenix').format('YYYY-MM-DD HH:mm')
                  

                  这篇关于在特定时区格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 JavaScript 中获取 UTC 时间戳? 下一篇:将客户端 javascript 时钟与服务器日期同步的最佳

                  相关文章

                  最新文章

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

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

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