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

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

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

        创建具有设定时区的日期而不使用字符串表示

        时间:2023-10-12
          <tfoot id='flQah'></tfoot><legend id='flQah'><style id='flQah'><dir id='flQah'><q id='flQah'></q></dir></style></legend>
              <tbody id='flQah'></tbody>
            <i id='flQah'><tr id='flQah'><dt id='flQah'><q id='flQah'><span id='flQah'><b id='flQah'><form id='flQah'><ins id='flQah'></ins><ul id='flQah'></ul><sub id='flQah'></sub></form><legend id='flQah'></legend><bdo id='flQah'><pre id='flQah'><center id='flQah'></center></pre></bdo></b><th id='flQah'></th></span></q></dt></tr></i><div id='flQah'><tfoot id='flQah'></tfoot><dl id='flQah'><fieldset id='flQah'></fieldset></dl></div>
                <bdo id='flQah'></bdo><ul id='flQah'></ul>

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

                1. 本文介绍了创建具有设定时区的日期而不使用字符串表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个网页,其中包含日、月和年的三个下拉菜单.如果我使用接受数字的 JavaScript Date 构造函数,那么我将获得当前时区的 Date 对象:

                  I have a web page with three dropdowns for day, month and year. If I use the JavaScript Date constructor that takes numbers, then I get a Date object for my current timezone:

                  new Date(xiYear, xiMonth, xiDate)
                  

                  给出正确的日期,但由于夏令时,它认为日期是 GMT+01:00.

                  Give the correct date, but it thinks that date is GMT+01:00 due to daylight savings time.

                  这里的问题是,然后我将此 Date 传递给 Ajax 方法,当日期在服务器上被反序列化时,它已被转换为 GMT,因此丢失了一个小时,从而将日期向后移动一.现在我可以将日、月和年单独传递到 Ajax 方法中,但似乎应该有更好的方法.

                  The problem here is that I then pass this Date to an Ajax method and when the date is deserialised on the server it has been converted to GMT and so lost an hour which moves the day back by one. Now I could just pass the day, month, and year individually into the Ajax method, but it seems that there ought to be a better way.

                  接受的答案为我指明了正确的方向,但是仅使用 setUTCHours() 本身就发生了变化:

                  The accepted answer pointed me in the right direction, however just using setUTCHours() by itself changed:

                  Apr 5th 00:00 GMT+01:00 
                  

                  Apr 4th 23:00 GMT+01:00
                  

                  然后我还必须设置 UTC 日期、月份和年份以结束

                  I then also had to set the UTC date, month and year to end up with

                  Apr 5th 01:00 GMT+01:00
                  

                  这正是我想要的.

                  推荐答案

                  使用 .setUTCHours() 可以在 UTC-time 中实际设置日期,这将允许您使用 UTC-整个系统的时间.

                  using .setUTCHours() it would be possible to actually set dates in UTC-time, which would allow you to use UTC-times throughout the system.

                  你不能在构造函数中使用 UTC 设置它,除非你指定一个日期字符串.

                  使用 new Date(Date.UTC(year, month, day, hour, minute, second)) 您可以从特定的 UTC 时间创建 Date-object.

                  Using new Date(Date.UTC(year, month, day, hour, minute, second)) you can create a Date-object from a specific UTC time.

                  这篇关于创建具有设定时区的日期而不使用字符串表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:将 UTC 日期时间转换为本地日期时间 下一篇:如何在 JavaScript 中使用 ISO 8601 格式化带有时区偏

                  相关文章

                  最新文章

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

                  2. <legend id='TLiH5'><style id='TLiH5'><dir id='TLiH5'><q id='TLiH5'></q></dir></style></legend>
                    <tfoot id='TLiH5'></tfoot>

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

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