Improve humanizeHours

This commit is contained in:
Ricardo Gonzalez
2020-10-11 17:45:10 +01:00
parent 0fffc167fb
commit 96534c44b7
5 changed files with 30 additions and 29 deletions
@@ -8,6 +8,7 @@ import { DashboardService } from 'app/modules/dashboard/dashboard.service';
import * as moment from "moment";
import {MatDialog} from '@angular/material/dialog';
import { DashboardSettingsComponent } from 'app/layout/common/dashboard-settings/dashboard-settings.component';
import humanizeDuration from 'humanize-duration'
@Component({
selector : 'example',
@@ -184,23 +185,16 @@ export class DashboardComponent implements OnInit, AfterViewInit, OnDestroy
return item.id || index;
}
humanizeHours(hours: number): string {
humanizeHours(hours: number, full: boolean = false): string {
let duration: string = ''
if(!hours){
return '--'
}
var value: number
let unit = ""
if(hours > (24*365)){ //more than a year
value = Math.round((hours/(24*365)) * 10)/10
unit = "years"
} else if (hours > 24){
value = Math.round((hours/24) *10 )/10
unit = "days"
} else{
value = hours
unit = "hours"
if(!full){
duration = humanizeDuration(hours * 60 * 60 * 1000, { round: true, largest: 1, units: ['y', 'd', 'h', 'm', 's'] });
} else {
duration = humanizeDuration(hours * 60 * 60 * 1000, { conjunction: " and ", serialComma: false });
}
return `${value} ${unit}`
return duration
}
}