Skip to content

Instantly share code, notes, and snippets.

@darrenmothersele
Last active January 9, 2022 09:10
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darrenmothersele/b8f6216a190d811042c33f1cabbb7813 to your computer and use it in GitHub Desktop.
Save darrenmothersele/b8f6216a190d811042c33f1cabbb7813 to your computer and use it in GitHub Desktop.
Duration Pipe for Angular 2, 4, 5 - converts ISO 8601 duration (eg, "PT1H30M5S") to 01:30:05
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'duration'
})
export class DurationPipe implements PipeTransform {
transform(value: string): string {
const leftPad = x => String(x).length >= 2 ? x : leftPad(`0${x}`);
const [ _, hours, mins, secs ] = value.match(/PT(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?/);
return [hours || 0, mins || 0, secs || 0].map(leftPad).join(':');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment