Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Abbreviated relative time (Instagram style) using momentjs?

moment().startOf('day').fromNow() //6 hours ago.

How can I change the above to show 6h instead?

like image 248
Prabhu Avatar asked Feb 28 '15 14:02

Prabhu


1 Answers

You can define custom locale strings.

moment.locale('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s:  "seconds",
        m:  "a minute",
        mm: "%d minutes",
        h:  "an hour",
        hh: "%d hours",
        d:  "a day",
        dd: "%d days",
        M:  "a month",
        MM: "%d months",
        y:  "a year",
        yy: "%d years"
    }
});

If you require additional processing, you can set the token as a function like shown below, the function should return a string.

function relativeTime(number, withoutSuffix, key, isFuture) {
    return aString;
}
like image 65
Etheryte Avatar answered Sep 21 '22 02:09

Etheryte