1 import { Pipe, PipeTransform } from '@angular/core';
3 import * as moment from 'moment';
9 export class DurationPipe implements PipeTransform {
10 transform(date: any, isRelative = false): string {
12 return moment(date).fromNow();
14 return this._forHumans(date);
19 * Translates seconds into human readable format of seconds, minutes, hours, days, and years
20 * source: https://stackoverflow.com/a/34270811
22 * @param {number} seconds The number of seconds to be processed
23 * @return {string} The phrase describing the the amount of time
25 _forHumans(seconds: number): string {
27 [`${Math.floor(seconds / 31536000)}`, 'years'],
28 [`${Math.floor((seconds % 31536000) / 86400)}`, 'days'],
29 [`${Math.floor((seconds % 86400) / 3600)}`, 'hours'],
30 [`${Math.floor((seconds % 3600) / 60)}`, 'minutes'],
31 [`${Math.floor(seconds % 60)}`, 'seconds']
35 for (let i = 0, max = levels.length; i < max; i++) {
36 if (levels[i][0] === '0') {
43 (levels[i][0] === '1' ? levels[i][1].substr(0, levels[i][1].length - 1) : levels[i][1]);
45 return returntext.trim() || '1 second';