Can I change the time range in moment.js for fromNow()
, so the for hours range is from 60 seconds to 59 minutes and likewise for others not (90 sec - 45 mins).
ref: Moment.js Time from now
Is there something similar to how you can change the lang:
moment.lang('en', {
relativeTime: {
future: 'Due in %s',
past: '%s ago',
s: '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',
},
});
Can I change the time range in moment.js for fromNow()
, so the for hours range is from 60 seconds to 59 minutes and likewise for others not (90 sec - 45 mins).
ref: Moment.js Time from now
Is there something similar to how you can change the lang:
moment.lang('en', {
relativeTime: {
future: 'Due in %s',
past: '%s ago',
s: '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',
},
});
Share
Improve this question
edited Jul 4, 2020 at 1:20
Penny Liu
17.6k5 gold badges86 silver badges108 bronze badges
asked Jul 4, 2013 at 14:54
LabithiotisLabithiotis
4,1997 gold badges31 silver badges47 bronze badges
2
-
2
It doesn't look like there is a way to do that easily: github./timrwood/moment/blob/develop/moment.js#L934 (that's where it makes the substitutions). You'd probably have to write a function to manipulate the output from
fromNow
. – kalley Commented Jul 4, 2013 at 15:04 -
Requested for a future version here. In the meantime, you could modify the
relativeTime
function in your own copy of the source code. – Matt Johnson-Pint Commented Jul 9, 2013 at 22:51
1 Answer
Reset to default 4duration.humanize
has thresholds which define when a unit is considered a minute, an hour and so on. For example, by default more than 45 seconds is considered a minute, more than 22 hours is considered a day and so on.
To change those cutoffs use moment.relativeTimeThreshold(unit, limit)
where limit is one of s
, m
, h
, d
, M
.
s
seconds least number of seconds to be considered a minutem
minutes least number of minutes to be considered an hourh
hours least number of hours to be considered a dayd
days least number of days to be considered a monthM
months least number of months to be considered a year
// Retrieve existing thresholds
moment.relativeTimeThreshold('s'); // 45
moment.relativeTimeThreshold('m'); // 45
moment.relativeTimeThreshold('h'); // 22
moment.relativeTimeThreshold('d'); // 26
moment.relativeTimeThreshold('M'); // 11
// Set new thresholds
moment.relativeTimeThreshold('s', 40);
moment.relativeTimeThreshold('m', 40);
moment.relativeTimeThreshold('h', 20);
moment.relativeTimeThreshold('d', 25);
moment.relativeTimeThreshold('M', 10);
NOTE: Retrieving thresholds was added in 2.8.1.