2016-03-25 9 views

Antwort

1

Nach ein paar Stunden habe ich das moment-duration-format Plugin entdeckt. Sie würden .format() für ein Dauerobjekt aufrufen und eine Zeichenfolge übergeben, die es zu dem formatiert, was Sie anzeigen möchten. Hier ist, was ich am Ende schriftlich:

function formatDuration(duration, format_string) { 
    var length; 
    if (format_string === "long") { 
    format = [ 
     duration.months() === 1 ? "Month" : "Months", 
     duration.days() === 1 ? "Day" : "Days", 
     duration.hours() === 1 ? "Hour" : "Hours", 
     duration.minutes() === 1 ? "Minute" : "Minutes", 
     duration.seconds() === 1 ? "Second" : "Seconds" 
    ]; 
    length = duration.format("M [" + format[0] + "] d [" + format[1] + 
    "] h [" + format[2] + "] m [" + format[3] + " and] s [" + format[4] + "]"); 
    } else if (format_string === "short") { 
    length = duration.format("M[m] d[d] h:mm:ss"); 
    } else { 
    length = duration.format(format_string); 
    }; 
    return length; 
}; 
0

Was dazu:

> var d = moment.duration(125, 's') 
undefined 
> moment().subtract(d).fromNow().replace(/ ago/, '') 
'2 minutes'