【问题标题】:Is there any way to get the difference between two datetime in moment.js有什么方法可以在 moment.js 中获取两个日期时间之间的差异
【发布时间】:2017-03-21 07:57:52
【问题描述】:

假设,我有两个 datetime moment 日期时间对象。

momentdatefirst = '2017-03-21T05:00:00+05:45'

momentdatesecond = '1990-03-21T07:12:45+05:45'

moment.js 中如何计算这两个时间戳之间的差异?

我试过了:

var diff_hour = momentdatefirst.diff(momentdatesecond); 

这并没有检索到实际的差异。

【问题讨论】:

标签: javascript datetime momentjs


【解决方案1】:

你应该这样做:

let date = moment('2017-03-21T05:00:00+05:45');
let dateTwo = moment('1990-03-21T07:12:45+05:45');
let diff = dateTwo.diff(date); // in milliseconds
let diffInHours = date.diff(dateTwo, 'hour'); // in hours
// and so on
console.log('diff in milliseconds', diff);

它将输出 852068835000 毫秒,大约 27 年。

【讨论】:

    【解决方案2】:
    var a = moment('2016-06-06T21:03:55');//now
    var b = moment('2016-05-06T20:03:55');
    
    console.log(a.diff(b, 'minutes')) // 44700
    console.log(a.diff(b, 'hours')) // 745
    console.log(a.diff(b, 'days')) // 31
    console.log(a.diff(b, 'weeks')) // 4
    

    您可以浏览时刻文档here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2020-03-22
      • 2017-03-27
      • 1970-01-01
      • 2012-04-18
      相关资源
      最近更新 更多