Seconds format like mm: ss in Angular 2

I show a few seconds that count 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 was hoping I could 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 does not work, as he expects to p.value.playerTimerbe an object of date or the number of seconds from an era.

Is there any other way to achieve this?

+4
source share
2 answers

. - . , , . , , - :

@Pipe({
  name: 'minuteSeconds'
})
export class MinuteSecondsPipe implements PipeTransform {

    transform(value: number): string {
       const minutes: number = Math.floor(value / 60);
       return minutes + ':' + (value - minutes * 60);
    }

}
+9

PierreDuc, / (, 00:01), ES2017 padStart :

return minutes.toString().padStart(2, '0') + ':' + (value - minutes * 60).toString().padStart(2, '0');

+1

Source: https://habr.com/ru/post/1685086/


All Articles