MomentJS - Relative Time Thresholds


Advertisements

This is used with duration.humanize where the length of duration is displayed as a few seconds ago, in a minute, an hour ago etc. No of seconds are predefined and displayed as few seconds ago, and the same is applicable for minute and hour. You can change the seconds, minute, hour, days limit using relative time threshold method.

Syntax

moment.relativeTimeThreshold(unit); // getter
moment.relativeTimeThreshold(unit, limit); // setter

The table given here shows the units used along with display message and description

Unit Display message Description
ss a few seconds least number of seconds to be considered seconds
s seconds least number of seconds to be considered as a minute
m minutes least number of minutes to be considered as a hour
h hours least number of hours to be considered as a day
d days Least number of days to be considered as a month
M months Least number of months to be considered as a year

Example

Observe the following code that displays the default time as few second, seconds, minutes, hours, days and months −

var m = moment.relativeTimeThreshold('ss');
var x = moment.relativeTimeThreshold('s');
var c = moment.relativeTimeThreshold('m');
var d = moment.relativeTimeThreshold('h');
var y = moment.relativeTimeThreshold('d');
var t = moment.relativeTimeThreshold('M');

Output

Thresholds

Note that minute threshold is changed from default 45 to 5 and the output for humanize for 6 minutes is displayed as in an hour

Example

moment.relativeTimeThreshold('m', 5);
var c = moment.duration(6, "minutes").humanize(true);

Output

relativeTimeThreshold

Minute threshold is changed from default 45 to 15 and the output for humanize for 6 minutes is displayed as in 6 minutes.

Example

moment.relativeTimeThreshold('m', 15);
var c = moment.duration(6, "minutes").humanize(true);

Output

Minute Threshold
momentjs_customization.htm
Advertisements