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
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, 2022Comments
-
Misha Moroshko almost 2 years
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 about 10 yearsThank for that! In case of "3 months" or "2 years", is that possible to format it using days, e.g. "90d" or "730d"?
-
jsmiff over 9 yearsThis doesn't have the correct syntax for every measure. For instance
mm: "%m"
should bemm: "%dm"
. -
Sebastian Bochan over 9 yearsAny 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 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 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 over 9 yearsDo you mean part with clone() and from() ?
-
cpk almost 9 yearsHas 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 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-40210140
-
catamphetamine over 6 yearsI all languages are required then this might be an option: github.com/catamphetamine/javascript-time-ago