首页 > 解决方案 > 如何在Angular 6模板中减去时间值

问题描述

我有两个时间值。一次从 API 获取,其他时间由用户输入。我想减去这两个值并显示时间差。如何在 Angular 6 中减去。

代码 -

<input type="datetime-local" formControlName="timeOfDispatch" class="form-control">
<input type="datetime-local" formControlName="factoryReceiveTime"(focusout)="calculateTime()" class="form-control">

角 -

const timeOfResponse = 
this.harvestWeighmentForm.controls.factoryReceiveTime.value;
const timeOfCall = this.harvestWeighmentForm.controls.timeOfDispatch.value;
console.log(timeOfResponse - timeOfCall);

当我这样做时,我得到了 NaN。

标签: htmlangularangular6

解决方案


这可以在 Typescript 中轻松完成。

function timeDifference(d1: Date, d2: Date):number {
  return +d1 - +d2;
}

这将返回以毫秒为单位的时间差。

让我知道它是否有帮助。


推荐阅读