Reputation: 2962
How would i change the ago message using angular-moment
. i use this angular-moment
library to show time ago message.
<span am-time-ago="created_at"></span>
It shows messages like "a few seconds ago".
How can we make this message shorter? Can we remove ago
from this message?
So the message would be like:
1sec , 1min, 12min , 1hour, 12hours, 2years etc.
I tried to do this using:
moment.lang('en', {
relativeTime : {
future: "in %s",
past: "%s ago",
s: "sec",
m: "1m",
mm: "%m",
h: "1h",
hh: "%h",
d: "1d",
dd: "%dd",
M: "1m",
MM: "%dm",
y: "1y",
yy: "%dy"
}
});
placed this thing in angular app.js
after module. It worked well for 1 sec, 1 min, 1 hr, etc..
but it shows %
in place of absolute time. like %min
, instead of 12min
.
Any suggestion?
Upvotes: 0
Views: 1624
Reputation: 9497
angular-moment
provides amTimeAgoConfig
, for specifying whether or not ago
should be displayed:
app.constant('amTimeAgoConfig', {
withoutSuffix: true
});
moment.js
allows us to define functions to format relativeTime
, and these seem to work with angular-moment
. In this example, I defined functions for seconds and minutes:
moment.lang('en', {
relativeTime: {
future: "in %s",
past: "%s ago",
s: function(number, withoutSuffix, key, isFuture) {
return number + ' sec';
},
m: "1m",
mm: function(number, withoutSuffix, key, isFuture) {
return number + 'm';
}
}
});
If you want to try the code, here it is on Plunker.
Upvotes: 2