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

19,885

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

Share:
19,885
Misha Moroshko
Author by

Misha Moroshko

I build products that make humans happier. Previously Front End engineer at Facebook. Now, reimagining live experiences at https://muso.live

Updated on July 28, 2022

Comments

  • Misha Moroshko
    Misha Moroshko almost 2 years

    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?

  • Misha Moroshko
    Misha Moroshko about 10 years
    Thank for that! In case of "3 months" or "2 years", is that possible to format it using days, e.g. "90d" or "730d"?
  • jsmiff
    jsmiff over 9 years
    This doesn't have the correct syntax for every measure. For instance mm: "%m" should be mm: "%dm".
  • Sebastian Bochan
    Sebastian Bochan over 9 years
    Any ideas how to set two different langs (I mean formats for minutes/seconds etc) for different controllers? In other words, In one box I need to display: 20 minutes ago, but in the second box, 20 min.
  • Nix
    Nix over 9 years
    @jsmiff I updated the above but its just an example of how to remap. Feel free to update them to whatever you want.
  • Nix
    Nix over 9 years
    @SebastianBochan I could recommend a hack (define an "abbreviated" locale) but i dont know of how to format timeago out of the box.
  • Sebastian Bochan
    Sebastian Bochan over 9 years
    Do you mean part with clone() and from() ?
  • cpk
    cpk almost 9 years
    Has anyone used this in an Ionic project? can't seem the get dates to format properly, as they just revert back the original "A few seconds ago" format.
  • KajMagnus
    KajMagnus over 8 years
    @MishaMoroshko Yes 90d instead of 3m (months) can apparently be "configured" as shown here: github.com/urish/angular-moment/issues/44#issuecomment-40210‌​140
  • catamphetamine
    catamphetamine over 6 years
    I all languages are required then this might be an option: github.com/catamphetamine/javascript-time-ago