在 Angular 2 中将秒数格式化为 mm:ss [英] Format number of seconds as mm:ss in Angular 2

查看:23
本文介绍了在 Angular 2 中将秒数格式化为 mm:ss的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在显示从 3600 倒计时到 0 的秒数.

"0">{{ p.value.playerTimer }}</div>

我想将秒数格式化为分:秒

我希望能够使用 DatePipe - https://angular.io/api/common/日期管道

"0">{{ p.value.playerTimer |日期:'彩信' }}</div>

但这不起作用,因为它期望 p.value.playerTimer 是一个日期对象或自纪元以来的秒数.

还有其他方法可以实现吗?

解决方案

Angular Date Pipe 也适用于数值,但请注意:只有毫秒.

如果您想获得 mm:ss(00:00),您需要将您的数值转换为毫秒.在您的情况下:3600 * 1000

{{ p.value.playerTimer * 1000 |日期:'mm:ss' }}

这里是 Stackblitz 的例子https://stackblitz.com/edit/date-pipe-example-nhafvx

也许有人会派上用场

I am displaying a number of seconds that counts down from 3600 to 0.

<div class="time-box" *ngIf="p.value.playerTimer > 0">{{ p.value.playerTimer }}</div>

I would like to format the number of seconds as minutes:seconds

I hoped to be able to use DatePipe - https://angular.io/api/common/DatePipe

<div class="time-box" *ngIf="p.value.playerTimer > 0">{{ p.value.playerTimer | date:'mmss' }}</div>

But this doesn't work as it expects p.value.playerTimer to be a date object or number of seconds since the epoch.

Is there another way to achieve this?

解决方案

Angular Date Pipe works with number value too, But please note: Only with milliseconds.

If you want to get mm:ss(00:00) you need to convert your number value to milliseconds. In your case: 3600 * 1000

<div class="time-box" *ngIf="p.value.playerTimer > 0">
    {{ p.value.playerTimer * 1000 | date:'mm:ss' }}
</div>

here is the Stackblitz example https://stackblitz.com/edit/date-pipe-example-nhafvx

maybe someone will come in handy

这篇关于在 Angular 2 中将秒数格式化为 mm:ss的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆