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

    <tfoot id='zgmbq'></tfoot>

  • <legend id='zgmbq'><style id='zgmbq'><dir id='zgmbq'><q id='zgmbq'></q></dir></style></legend>

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

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

        如何将 Moment.js 日期转换为用户本地时区?

        时间:2023-06-14

            <tbody id='f3Bzp'></tbody>
        • <small id='f3Bzp'></small><noframes id='f3Bzp'>

              <bdo id='f3Bzp'></bdo><ul id='f3Bzp'></ul>
              • <legend id='f3Bzp'><style id='f3Bzp'><dir id='f3Bzp'><q id='f3Bzp'></q></dir></style></legend>

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

                  本文介绍了如何将 Moment.js 日期转换为用户本地时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用 Moment.js 和 Moment-Timezone 框架,并且有一个 Moment.js 日期对象,该对象明确使用 UTC 时区.如何将其转换为浏览器的当前时区?

                  I use the Moment.js and Moment-Timezone frameworks, and have a Moment.js date object which is explicitly in UTC timezone. How can I convert that to the current timezone of the browser?

                  var testDateUtc = moment.tz("2015-01-30 10:00:00", "UTC");var localDate = ???

                  所以如果我能找出用户的本地时区就好了;或者,我想将日期对象转换为另一个只使用本地时区"的数据对象,无论它实际上是什么.

                  So it would be fine if I could find out the users local time zone; or alternatively I'd like to convert the date object into another data object which just uses the "local timezone", no matter what that actually is.

                  推荐答案

                  您不需要为此使用 moment-timezone.主要的 moment.js 库具有使用 UTC 和本地时区的完整功能.

                  You do not need to use moment-timezone for this. The main moment.js library has full functionality for working with UTC and the local time zone.

                  var testDateUtc = moment.utc("2015-01-30 10:00:00");
                  var localDate = moment(testDateUtc).local();
                  

                  从那里您可以使用您可能期望的任何功能:

                  From there you can use any of the functions you might expect:

                  var s = localDate.format("YYYY-MM-DD HH:mm:ss");
                  var d = localDate.toDate();
                  // etc...
                  

                  请注意,通过将 testDateUtc(它是一个 moment 对象)传回 moment() 构造函数,它会创建一个 克隆.否则,当您调用 .local() 时,它也会更改 testDateUtc 值,而不仅仅是 localDate 值.时刻是可变的.

                  Note that by passing testDateUtc, which is a moment object, back into the moment() constructor, it creates a clone. Otherwise, when you called .local(), it would also change the testDateUtc value, instead of just the localDate value. Moments are mutable.

                  另外请注意,如果您的原始输入包含时区偏移量,例如 +00:00Z,那么您可以直接使用 moment 解析它.您不需要使用 .utc.local.例如:

                  Also note that if your original input contains a time zone offset such as +00:00 or Z, then you can just parse it directly with moment. You don't need to use .utc or .local. For example:

                  var localDate = moment("2015-01-30T10:00:00Z");
                  

                  这篇关于如何将 Moment.js 日期转换为用户本地时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                        <tbody id='WwVg3'></tbody>
                      • <bdo id='WwVg3'></bdo><ul id='WwVg3'></ul>

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