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

    <tfoot id='M5fi4'></tfoot>

    1. <legend id='M5fi4'><style id='M5fi4'><dir id='M5fi4'><q id='M5fi4'></q></dir></style></legend>
      • <bdo id='M5fi4'></bdo><ul id='M5fi4'></ul>

        <i id='M5fi4'><tr id='M5fi4'><dt id='M5fi4'><q id='M5fi4'><span id='M5fi4'><b id='M5fi4'><form id='M5fi4'><ins id='M5fi4'></ins><ul id='M5fi4'></ul><sub id='M5fi4'></sub></form><legend id='M5fi4'></legend><bdo id='M5fi4'><pre id='M5fi4'><center id='M5fi4'></center></pre></bdo></b><th id='M5fi4'></th></span></q></dt></tr></i><div id='M5fi4'><tfoot id='M5fi4'></tfoot><dl id='M5fi4'><fieldset id='M5fi4'></fieldset></dl></div>
      1. 如何在 JavaScript 中获取 UTC 时间戳?

        时间:2023-10-12
        • <small id='veNRj'></small><noframes id='veNRj'>

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

                • <bdo id='veNRj'></bdo><ul id='veNRj'></ul>
                  <legend id='veNRj'><style id='veNRj'><dir id='veNRj'><q id='veNRj'></q></dir></style></legend>
                    <tbody id='veNRj'></tbody>
                • 本文介绍了如何在 JavaScript 中获取 UTC 时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在编写 Web 应用程序时,将(服务器端)所有日期时间作为 UTC 时间戳存储在数据库中是有意义的.

                  While writing a web application, it makes sense to store (server side) all datetimes in the DB as UTC timestamps.

                  当我注意到在 JavaScript 中的时区操作方面,您本身无法做很多事情时,我感到很惊讶.

                  I was astonished when I noticed that you couldn't natively do much in terms of Timezone manipulation in JavaScript.

                  我稍微扩展了 Date 对象.这个功能有意义吗?基本上,每次我向服务器发送任何东西时,它都会是一个用这个函数格式化的时间戳......

                  I extended the Date object a little. Does this function make sense? Basically, every time I send anything to the server, it's going to be a timestamp formatted with this function...

                  你能看出这里有什么大问题吗?还是换个角度的解决方案?

                  Can you see any major problems here? Or maybe a solution from a different angle?

                  Date.prototype.getUTCTime = function(){ 
                    return new Date(
                      this.getUTCFullYear(),
                      this.getUTCMonth(),
                      this.getUTCDate(),
                      this.getUTCHours(),
                      this.getUTCMinutes(), 
                      this.getUTCSeconds()
                    ).getTime(); 
                  }
                  

                  这对我来说似乎有点令人费解.我也不太确定性能.

                  It just seems a little convoluted to me. And I am not so sure about performance either.

                  推荐答案

                  1. 以这种方式构造的日期使用本地时区,导致构造的日期不正确.设置某个日期对象的时区是从包含时区的日期字符串构造它.(我无法让它在旧版 Android 浏览器中运行.)

                  1. Dates constructed that way use the local timezone, making the constructed date incorrect. To set the timezone of a certain date object is to construct it from a date string that includes the timezone. (I had problems getting that to work in an older Android browser.)

                  请注意,getTime() 返回毫秒,而不是普通秒.

                  Note that getTime() returns milliseconds, not plain seconds.

                  对于 UTC/Unix 时间戳,以下内容就足够了:

                  For a UTC/Unix timestamp, the following should suffice:

                  Math.floor((new Date()).getTime() / 1000)
                  

                  它会将当前时区偏移量计入结果中.对于字符串表示,David Ellis' 答案有效.

                  It will factor the current timezone offset into the result. For a string representation, David Ellis' answer works.

                  澄清一下:

                  new Date(Y, M, D, h, m, s)
                  

                  该输入被视为当地时间.如果传入UTC时间,结果会有所不同.观察(我现在在 GMT +02:00,现在是 07:50):

                  That input is treated as local time. If UTC time is passed in, the results will differ. Observe (I'm in GMT +02:00 right now, and it's 07:50):

                  > var d1 = new Date();
                  > d1.toUTCString();
                  "Sun, 18 Mar 2012 05:50:34 GMT" // two hours less than my local time
                  > Math.floor(d1.getTime()/ 1000)
                  1332049834 
                  
                  > var d2 = new Date( d1.getUTCFullYear(), d1.getUTCMonth(), d1.getUTCDate(), d1.getUTCHours(), d1.getUTCMinutes(), d1.getUTCSeconds() );
                  > d2.toUTCString();
                  "Sun, 18 Mar 2012 03:50:34 GMT" // four hours less than my local time, and two hours less than the original time - because my GMT+2 input was interpreted as GMT+0!
                  > Math.floor(d2.getTime()/ 1000)
                  1332042634
                  

                  另请注意,getUTCDate() 不能替代 getUTCDay().这是因为 getUTCDate() 返回 一个月中的哪一天;而 getUTCDay() 返回 星期几.

                  Also note that getUTCDate() cannot be substituted for getUTCDay(). This is because getUTCDate() returns the day of the month; whereas, getUTCDay() returns the day of the week.

                  这篇关于如何在 JavaScript 中获取 UTC 时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 JavaScript 中使用 ISO 8601 格式化带有时区偏 下一篇:在特定时区格式化日期

                  相关文章

                  最新文章

                    <tfoot id='UNTeV'></tfoot>
                    • <bdo id='UNTeV'></bdo><ul id='UNTeV'></ul>

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

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