我知道我可以做任何事情,还有一些更复杂的约会。但令人尴尬的是,我在尝试做一些看似简单的事情时遇到了困难:得到2倍之间的差异。

例子:

var now  = "04/09/2013 15:00:00";
var then = "04/09/2013 14:20:30";

//expected result:
"00:39:30"

我所做的:

var now = moment("04/09/2013 15:00:00");
var then = moment("04/09/2013 14:20:30");

console.log(moment(moment.duration(now.diff(then))).format("hh:mm:ss"))
//outputs 10:39:30  

我不明白这里的“10”是什么。我住在巴西,所以我们是utc-0300。

moment.duration(now.diff(then))的结果是一个具有正确内部值的持续时间:

 days: 0
 hours: 0
 milliseconds: 0
 minutes: 39
 months: 0
 seconds: 30
 years: 0

所以,我想我的问题是:如何将一个momentjs Duration转换为一个时间间隔?我肯定可以用

duration.get("hours") +":"+ duration.get("minutes") +:+ duration.get("seconds")

但我觉得有一些更优雅的东西,我完全错过了。

更新 仔细看,在上面的例子中是:

Tue Apr 09 2013 15:00:00 GMT-0300 (E. South America Standard Time)…}

而moment(moment.duration(now.diff(then)))为:

Wed Dec 31 1969 22:39:30 GMT-0200 (E. South America Daylight Time)…}

我不确定为什么第二个值在夏令时(-0200)…但我确定我不喜欢约会:(

更新2

好吧,这个值是-0200,可能是因为31/12/1969是使用日光时间的日期……就是这样。


当前回答

基于日期时间的输入

    var dt1 = new Date("2019-1-8 11:19:16");
    var dt2 = new Date("2019-1-8 11:24:16");


    var diff =(dt2.getTime() - dt1.getTime()) ;
    var hours = Math.floor(diff / (1000 * 60 * 60));
    diff -= hours * (1000 * 60 * 60);
    var mins = Math.floor(diff / (1000 * 60));
    diff -= mins * (1000 * 60);


    var response = {
        status : 200,
        Hour : hours,
        Mins : mins
    }

输出

{
"status": 200,
"Hour": 0,
"Mins": 5
}

其他回答

如果您希望将两个时间戳的差异转换为总天数、小时和分钟,而不是月和年。

var now  = "01/08/2016 15:00:00";
var then = "04/02/2016 14:20:30";
var diff = moment.duration(moment(then).diff(moment(now)));

Diff包含2个月23天23小时20分钟。但我们只需要在几天、几小时和几分钟内得到结果,所以简单的解决方案是:

var days = parseInt(diff.asDays()); //84

var hours = parseInt(diff.asHours()); //2039 hours, but it gives total hours in given miliseconds which is not expacted.

hours = hours - days*24;  // 23 hours

var minutes = parseInt(diff.asMinutes()); //122360 minutes,but it gives total minutes in given miliseconds which is not expacted.

minutes = minutes - (days*24*60 + hours*60); //20 minutes.

最终结果是:84天23小时20分钟。

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') //[days, years, months, seconds, ...]
//Result 1 

为我工作

详情见 http://momentjs.com/docs/#/displaying/difference/

用这个:

var duration = moment.duration(endDate.diff(startDate));
var aa = duration.asHours();

使用momentjs的Epoch时差:

获取两个epoch的区别:

语法: moment.duration(时刻(时刻(date1) .diff(时刻(date2)))) .asHours ()

工时差异: moment.duration(时刻(时刻(1590597744551).diff(时刻(1590597909877)))).asHours ()

分钟差: moment.duration(时刻(时刻(1590597744551).diff(时刻(1590597909877)))).asMinutes () .toFixed ()

注意:如果需要精确的值,可以删除. tofixed()。

代码:

const moment = require('moment')

console.log('Date 1',moment(1590597909877).toISOString())
console.log('Date 2',moment(1590597744551).toISOString())
console.log('Date1 - Date 2 time diffrence is : ',moment.duration(moment(moment(1590597909877).diff(moment(1590597744551)))).asMinutes().toFixed()+' minutes')

参考工作示例: https://repl.it/repls/MoccasinDearDimension

以下方法适用于所有情况(日期之间的差异小于24小时和差异大于24小时):

// Defining start and end variables
let start = moment('04/09/2013 15:00:00', 'DD/MM/YYYY hh:mm:ss');
let end   = moment('04/09/2013 14:20:30', 'DD/MM/YYYY hh:mm:ss');

// Getting the difference: hours (h), minutes (m) and seconds (s)
let h  = end.diff(start, 'hours');
let m  = end.diff(start, 'minutes') - (60 * h);
let s  = end.diff(start, 'seconds') - (60 * 60 * h) - (60 * m);

// Formating in hh:mm:ss (appends a left zero when num < 10)
let hh = ('0' + h).slice(-2);
let mm = ('0' + m).slice(-2);
let ss = ('0' + s).slice(-2);

console.log(`${hh}:${mm}:${ss}`); // 00:39:30