Abbreviated relative time (Instagram style) using momentjs?

datetime, javascript, momentjs

Solution

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;
}

Problem

``` moment().startOf('day').fromNow() //6 hours ago. ``` How can I change the above to show 6h instead?

Original source