用两个小数位创建运行时

时间:2019-05-15 19:30:17

标签: angular typescript intervals

我需要创建一个经过的时间,并在屏幕上为用户显示如下: 00:00:00

此刻,我正在按照下面的方式进行操作,但是它采用的格式为 0:0:0 ,我需要使用 01:03:59 < / strong>。

用户单击开始并开始计算经过的时间。

我该怎么做?

在打字稿中:

this.duration = {
  hours: 0,
  minutes: 0,
  seconds: 0
};



start(): void {
  const counter = setInterval(() => {

  this.duration.seconds += 1;

  if (this.duration.minutes === 60) {
    this.duration.hour += 1;
  }

  if (this.duration.seconds === 60) {
    this.duration.minutes += 1;
  }

  if (this.duration.seconds === 60) {
    this.duration.seconds = 0;
  }

}, 1000)

}

在html中

{{ duration.hours }}:{{ duration.minutes }}:{{ duration.seconds }}

3 个答案:

答案 0 :(得分:1)

您可以为每个零件使用现有的角度数字管道

{{ duration.hours | number:'2.0-0'}}:{{ duration.minutes | number:'2.0-0'}}:{{ duration.seconds | number:'2.0-0'}}

这样,如果您的小时数超过99小时,它将正确显示


{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}.

minIntegerDigits: The minimum number of integer digits before the decimal point. Default is 1.
minFractionDigits: The minimum number of digits after the decimal point. Default is 0.
maxFractionDigits: The maximum number of digits after the decimal point. Default is 3.

工作Demo

答案 1 :(得分:0)

您好,您可以使用 Angular pipe

<div>{{time| date:'HH:mm:ss'}}</div> 12:01:59

答案 2 :(得分:0)

您可以编写自己的管道,如下所示:

import pandas as pd

data = {'name': ['Lisa', 'Lisa', 'Mac', 'Intosh'],
    'ear': ['Right','Left','Right','Left']
   }

df = pd.DataFrame(data, columns = ['name', 'ear'])
df.loc[(df['name'] == 'Right') & (df['ear'] == 'Left')]
df

这会将您的数字转换为模板中的字符串,但是我认为这不会引起问题。