I use moment.js to get relative time. It returns, for example, "6 hours ago". But I would like to get the short version like "6h".
I read the doc: http://momentjs.com/docs/#/customization/relative-time/
But if I change:
moment.updateLocale('en', {
relativeTime : {
future: "in %s",
past: "%s ago",
s : 'a few seconds',
ss : '%d seconds',
m: "a minute",
mm: "%d 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"
}
});
to
moment.updateLocale('en', {
relativeTime : {
future: "in %s",
past: "%s",
s : 'a few seconds',
ss : '%d h',
m: "a minute",
mm: "%d m",
h: "an hour",
hh: "%d h",
d: "a day",
dd: "%d d",
M: "a month",
MM: "%d m",
y: "a year",
yy: "%d y"
}
});
I get the error:
Cannot read property 'humanize' of undefined at Moment.from (moment.js:3313)
When I call
moment(value).fromNow()
Here, value is a date as Date type
Is it possible to get short format version with moment.js ?
I assume your issue is the format string. I used 'hh':
moment.updateLocale('en', {
relativeTime : {
future: "in %s",
past: "%s ago",
s : 'a few seconds',
ss : '%d seconds',
m: "a minute",
mm: "%d minutes",
h: "an hour",
hh: "%dh",
d: "a day",
dd: "%d days",
M: "a month",
MM: "%d months",
y: "a year",
yy: "%d years"
}
});
//
// compute six hours ago...
//
var value = new Date();
value.setHours(value.getHours() - 6);
console.log('With \'hh\' format string: ' + moment(value).fromNow('hh'));
console.log('With no format: ' + moment(value).fromNow());
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With