Saturday, May 25, 2024
 Popular · Latest · Hot · Upcoming
115
rated 0 times [  117] [ 2]  / answers: 1 / hits: 32674  / 8 Years ago, thu, july 14, 2016, 12:00:00

If I use



moment().startOf(minute).fromNow();


I will get:



a few seconds ago
a minute ago
...


Is it possible to format the output like the following?



00:00 minutes ago
00:01 minutes ago
...

More From » momentjs

 Answers
22

You can customize how moment formats relative time for you locale using updateLocale.



Note that the docs says:




If a locale requires additional processing for a token, it can set the token as a function with the following signature. The function should return a string.




function (number, withoutSuffix, key, isFuture) {
return string;
}


In your case, you can do something like this:





var m1 = moment().subtract(5, 'm');
var m2 = moment().subtract(15, 's');

console.log(m1.fromNow());
console.log(m2.fromNow());

moment.updateLocale('en', {
relativeTime : {
future: in %s,
past: %s ago,
s: function (number, withoutSuffix, key, isFuture){
return '00:' + (number<10 ? '0':'') + number + ' minutes';
},
m: 01:00 minutes,
mm: function (number, withoutSuffix, key, isFuture){
return (number<10 ? '0':'') + number + ':00' + ' 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
}
});


console.log(m1.fromNow());
console.log(m2.fromNow());

<script src=//cdnjs.cloudflare.com/ajax/libs/moment.js/2.14.1/moment.min.js></script>





I'm not sure that the code above covers all the case you need, but I think that it can be a good starting point.


[#61383] Tuesday, July 12, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kentrelle

Total Points: 333
Total Questions: 93
Total Answers: 95

Location: Vietnam
Member since Sun, Oct 18, 2020
4 Years ago
;