两个不同的小时相减 Angular 6

two different hours subtraction Angular 6

我正在尝试如何减去捕获的两个不同时间:

这是TS代码

clockingIn() {
    var dt = new Date()
    this.clockedIn= new Date().getHours()+':'+ new Date().getMinutes()+':'+ new Date().getSeconds();
    console.log('clockin', this.clockedIn);
    this.disableClockIn = true;
    this.disableClockOut = false;
}

clockingOut() {
    var dt2 = new Date();
    this.clockedOut= new Date().getHours()+':'+ new Date().getMinutes()+':'+ new Date().getSeconds();
    console.log('clockout', this.clockedOut);
    this.disableClockIn = false;
    this.disableClockOut = true;
}

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

    var ms = moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"));
    var d = moment.duration(ms);
}

//table 

test() {
    const data = {
      dateClock: this.dateobj(),
      Job: this.selectedJob,
      clockI: this.clockedIn,
      clockO:  this.clockedOut,
      Hoursworked: this.subtraction()
    };

    this.dataSource.data.push(data);
    this.refresh();
    console.log(this.dataSource);
}

所以这是打卡和打卡。

我希望这两个可以计算(下班时间 - 下班时间)以查看我制作的 table 中两个小时的差异。这样一来,我就可以看到 Clock Out 和 Clock In 之间的差异时间。

我假设你的持续时间将在 subtraction 函数中计算。

以下更改将得到结果:

subtraction() {
    var now  = "04/09/2013 15:00:00";
    var then = "02/09/2013 14:20:30";
    // I have set to minutes. Change unit as per your requirement by 
    // referring doc given as reference
    var unit = 'm'; 

    return moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"), unit);
}

Moment.js duration