我知道我可以做任何事情,还有一些更复杂的约会。但令人尴尬的是,我在尝试做一些看似简单的事情时遇到了困难:得到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是使用日光时间的日期……就是这样。


当前回答

使用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

其他回答

亲密的= 06:38 Outtime = 15:40

 calTimeDifference(){
        this.start = dailyattendance.InTime.split(":");
        this.end = dailyattendance.OutTime.split(":");
        var time1 = ((parseInt(this.start[0]) * 60) + parseInt(this.start[1]))
        var time2 = ((parseInt(this.end[0]) * 60) + parseInt(this.end[1]));
        var time3 = ((time2 - time1) / 60);
        var timeHr = parseInt(""+time3);
        var  timeMin = ((time2 - time1) % 60);
    }
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/

如果你想要两个日期(startDate, endDate)之间的本地化天数:

var currentLocaleData = moment.localeData("en");
var duration = moment.duration(endDate.diff(startDate));
var nbDays = Math.floor(duration.asDays()); // complete days
var nbDaysStr = currentLocaleData.relativeTime(returnVal.days, false, "dd", false);

nbDaysStr将包含类似“3天”的内容;

例如,有关如何显示小时数或月数的信息,请参阅https://momentjs.com/docs/#/i18n/changing-locale/。

如果我们只想要hh:mm:ss,我们可以使用这样的函数:

//param: duration in milliseconds
MillisecondsToTime: function(duration) {
    var seconds = parseInt((duration/1000)%60)
        , minutes = parseInt((duration/(1000*60))%60)
        , hours = parseInt((duration/(1000*60*60))%24)
        , days  = parseInt(duration/(1000*60*60*24));

    var hoursDays = parseInt(days*24);
    hours += hoursDays;
    hours = (hours < 10) ? "0" + hours : hours;
    minutes = (minutes < 10) ? "0" + minutes : minutes;
    seconds = (seconds < 10) ? "0" + seconds : seconds;
    return hours + ":" + minutes + ":" + seconds;
}

以下方法适用于所有情况(日期之间的差异小于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