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


当前回答

亲密的= 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/

Typescript:以下应该工作,

export const getTimeBetweenDates = ({
  until,
  format
}: {
  until: number;
  format: 'seconds' | 'minutes' | 'hours' | 'days';
}): number => {
  const date = new Date();
  const remainingTime = new Date(until * 1000);
  const getFrom = moment([date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate()]);
  const getUntil = moment([remainingTime.getUTCFullYear(), remainingTime.getUTCMonth(), remainingTime.getUTCDate()]);
  const diff = getUntil.diff(getFrom, format);
  return !isNaN(diff) ? diff : null;
};

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

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分钟。

而不是

Math.floor(duration.asHours()) + moment.utc(duration.asMilliseconds()).format(":mm:ss")

最好是去做

moment.utc(total.asMilliseconds()).format("HH:mm:ss");
const getRemainingTime = (t2) => {
  const t1 = new Date().getTime();
  let ts = (t1-t2.getTime()) / 1000;

  var d = Math.floor(ts / (3600*24));
  var h = Math.floor(ts % (3600*24) / 3600);
  var m = Math.floor(ts % 3600 / 60);
  var s = Math.floor(ts % 60);

  console.log(d, h, m, s)

}