【问题标题】:How to subtract time values in Angular 6 template如何在Angular 6模板中减去时间值
【发布时间】:2020-08-25 09:33:04
【问题描述】:

我有两个时间值。一次从 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。

【问题讨论】:

  • 到目前为止,您尝试了哪些方法?一般来说,发布您当前的代码、您尝试过的内容以及您看到的当前错误会很有帮助。

标签: html angular angular6


【解决方案1】:

这可以在 Typescript 中轻松完成。

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

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

如果有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 2020-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多