如何计算Angular 2中时间的差异

时间:2017-10-24 06:32:02

标签: javascript angular datetime

我得到了这个格式的In-time:11:08:43和Out-time:10:21:06我想要计算这两次之间的差异。两个时间的类型都是字符串。在Angular 2中怎么可能?

for (let item of this.list) {
    if (item.pin.length == 2) {
        var inTime = item.pin[0].time;
        var outTime = item.pin[1].time;
        let diffInMs: number = Date.parse(outTime) - Date.parse(inTime);
        let diffInHours: number = diffInMs / 1000 / 60 / 60;
        console.log(diffInHours);
    }
}

3 个答案:

答案 0 :(得分:0)

我想这是一个重复的问题:Date difference

对于user630209,正确的答案是

DATE_FORMAT(date, format_mask)
Example:
SELECT DATE_FORMAT("2017-06-15", "%M %d %Y");
SELECT DATE_FORMAT(BirthDate, "%W %M %e %Y") FROM Employees;

使用Math.floor向下舍入结果,或使用Math.ceil将其向上舍入

答案 1 :(得分:0)

  if (item.pin.length == 2) {
      var inTime = item.pin[0].time;
      var outTime = item.pin[1].time;

      let IN = new Date(` ${item.date} ${inTime}`);
      let OUT = new Date(` ${item.date} ${outTime}`);
      let diff = OUT.getTime() - IN.getTime();
      var msec = diff;
      var hh = Math.floor(msec / 1000 / 60 / 60);
      msec -= hh * 1000 * 60 * 60;
      var mm = Math.floor(msec / 1000 / 60);
      msec -= mm * 1000 * 60;
      var ss = Math.floor(msec / 1000);
      msec -= ss * 1000;
      this.totalHour.push(hh + ":" + mm + ":" + ss);
    }

答案 2 :(得分:0)

getDataDiff(startDate, endDate) { 
    var diff = endDate.getTime() - startDate.getTime();
    var days = Math.floor(diff / (60 * 60 * 24 * 1000));
    var hours = Math.floor(diff / (60 * 60 * 1000)) - (days * 24);
    var minutes = Math.floor(diff / (60 * 1000)) - ((days * 24 * 60) + (hours * 60));
    var seconds = Math.floor(diff / 1000) - ((days * 24 * 60 * 60) + (hours * 60 * 60) + (minutes * 60));
    console.log(days, hours, minutes, seconds);
}