【问题标题】:How to get the difference in miliseconds between two Timestamps which are of different timezone in Javascript如何在Javascript中获得不同时区的两个时间戳之间的毫秒差异
【发布时间】:2017-07-25 06:04:54
【问题描述】:

我想在javascript中获取不同时区的两个时间戳的差异:

var firstTime ="2017-07-21 04:34:21"; 
var timeZone1:"+0100"; 
var secondTime ="2017-07-21 04:36:27";     
var timeZone2:"+0124";    

我们怎样才能得到以毫秒为单位的差异?

【问题讨论】:

标签: javascript


【解决方案1】:

将您的字符串转换为ISO 8601 格式。这些通常在 JavaScript Date 构造函数中运行良好。然后简单地从另一个中减去一个

var firstTime ="2017-07-21T04:34:21"; 
var timeZone1 = "+01:00"; 
var secondTime = "2017-07-21T04:36:27";     
var timeZone2 = "+01:24";  

const firstDate  = new Date(firstTime + timeZone1)
const secondDate = new Date(secondTime + timeZone2)
console.log('firstDate', firstDate)
console.log('secondDate', secondDate)
console.info('diff', firstDate - secondDate)

【讨论】:

    【解决方案2】:

    您可以使用 moment.js (https://momentjs.com/) 从不同时区获取当前时间戳。

    但不要忘记:“时区!= 偏移量” https://stackoverflow.com/tags/timezone/info

    另一个很好的例子: Get timezone difference in hours with moment

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-01
      • 2014-05-04
      • 1970-01-01
      • 2012-07-22
      • 1970-01-01
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多