Format number of seconds as mm:ss in Angular 2
You have to write your own pipe. Something like this. Be aware though, it's untested and does not take into account any strange input it might receive. Also it does not have any leading zeros, but hey, now you got something to do as well:
@Pipe({
name: 'minuteSeconds'
})
export class MinuteSecondsPipe implements PipeTransform {
transform(value: number): string {
const minutes: number = Math.floor(value / 60);
return minutes + ':' + (value - minutes * 60);
}
}
To put all the answers together plus a sample usage in HTML:
From markau and PierreDuc:
@Pipe({
name: 'minuteSeconds'
})
export class MinuteSecondsPipe implements PipeTransform {
transform(value: number): string {
const minutes: number = Math.floor(value / 60);
return minutes.toString().padStart(2, '0') + ':' +
(value - minutes * 60).toString().padStart(2, '0');
}
}
Then in your html:
<div ...>{{ p.value.playerTimer | minuteSeconds }}</div>
So for example, if p.value.playerTimer = 127, it will show 02:07
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