Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format 'angular-moment's 'am-time-ago' directive?

LIVE DEMO

I use the am-time-ago directive to show a relative timestamp:

<span am-time-ago="publishedAt"></span>

By default, it is formatted as "a day ago", "5 days ago", etc.

How could I change the formatting to be "1d", "5d", "3h", etc?

like image 466
Misha Moroshko Avatar asked Apr 09 '14 13:04

Misha Moroshko


1 Answers

You could customize humanize, somewhere in your config or app start.

moment.lang('en', {
    relativeTime : {
        future: "in %s",
        past:   "%s ago",
        s:  "%d seconds",
        m:  "1m",
        mm: "%dm",
        h:  "1h",
        hh: "%dh",
        d:  "1d",
        dd: "%dd",
        M:  "1m",
        MM: "%dm",
        y:  "1y",
        yy: "%dy"
    }
});

x = new moment();
z = x.clone().add('hours',1);
x.from(z, false);
>> 1h ago
x.from(z, true) //no ago
>> 1h

Docs on realtiveTime

Example: http://jsbin.com/satohazu/1/edit

like image 170
Nix Avatar answered Nov 19 '22 06:11

Nix