Correctly format video duration
This commit is contained in:
parent
a31bec5155
commit
03db5c3f97
|
@ -1,19 +1,28 @@
|
||||||
import { Pipe, PipeTransform } from '@angular/core'
|
import { Pipe, PipeTransform } from '@angular/core'
|
||||||
|
import { I18n } from '@ngx-translate/i18n-polyfill'
|
||||||
// Thanks: https://stackoverflow.com/a/46055604
|
|
||||||
|
|
||||||
@Pipe({
|
@Pipe({
|
||||||
name: 'myVideoDurationFormatter'
|
name: 'myVideoDurationFormatter'
|
||||||
})
|
})
|
||||||
export class VideoDurationPipe implements PipeTransform {
|
export class VideoDurationPipe implements PipeTransform {
|
||||||
|
|
||||||
|
constructor (private i18n: I18n) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
transform (value: number): string {
|
transform (value: number): string {
|
||||||
const minutes = Math.floor(value / 60)
|
const hours = Math.floor(value / 3600)
|
||||||
const hours = Math.floor(minutes / 60)
|
const minutes = Math.floor((value % 3600) / 60)
|
||||||
|
const seconds = value % 60
|
||||||
|
|
||||||
if (hours > 0) {
|
if (hours > 0) {
|
||||||
return hours + ' h ' + (minutes - hours * 60) + ' min ' + (value - (minutes - hours * 60) * 60) + ' sec'
|
return this.i18n('{{hours}} h {{minutes}} min {{seconds}} sec', { hours, minutes, seconds })
|
||||||
}
|
}
|
||||||
|
|
||||||
return minutes + ' min ' + (value - minutes * 60) + ' sec'
|
if (minutes > 0) {
|
||||||
|
return this.i18n('{{minutes}} min {{seconds}} sec', { minutes, seconds })
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.i18n('{{seconds}} sec', { seconds })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue