duration()

in ui-modules/utils/utils/momentp.js [66:131]


    duration(millis) {
        if (millis === 0) return this.capitalize("0ms");
        if (!millis) return this.capitalize("-");
        let tweak = x=>this.capitalize(x);
        if (millis < 0) {
            millis = -millis;
            tweak = x=> {
                return "- " + this.capitalize(x);
            }
        }

        if (millis < 1000) return tweak(millis+"ms");

        let secs = millis/1000;
        let secsR = Math.round(secs);
        if (secs < 10) return tweak(rounded(secs, 1)+"s");
        if (secs < 60) return tweak(secsR+"s");

        let mins = Math.floor(secs/60);
        let minsR = Math.round(secs/60);
        secs = Math.round(secs - mins*60);
        if (secs>=60) {
            mins++;
            secs -= 60;
        }
        if (mins < 5) {
            return tweak(mins +"m" + " " + secs +"s");
        }
        if (mins < 60) {
            return tweak(minsR) +" mins";
        }

        let hours = Math.floor(mins/60);
        let hoursR = Math.round(mins/60);
        mins = Math.round(mins - hours*60);
        if (mins >= 60) {
            hours++;
            mins -= 60;
        }
        if (hours < 4) return tweak(hours +"h" +" " + mins +"m");
        if (hours < 24) return tweak(hoursR) +" hours";

        let days = Math.floor(hours/24);
        let daysR = Math.round(hours/24);
        hours = Math.round(hours - days*24);
        if (hours >= 24) {
            days++;
            hours -= 24;
        }
        if (days < 7) return tweak(days + "d" +" " + hours +"h");
        if (days < 365) return tweak(daysR) + " days";

        let years = Math.floor(days / 365.25);
        let yearsR = Math.round(days / 365.25);
        days = Math.round(days - years*365.25);
        if (days >= 365) {
            years += 1;
            days -= 365;
        }
        if (years<=0) {
            years = 1;
            days = 0;
        }
        if (years < 10) return tweak(years + "y" +" " + days +"d");
        return tweak(yearsR) +" years";
    }