Open In App

Moment.js Customize Relative Time Thresholds

Last Updated : 30 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

This article will discuss the moment.js Customize Relative Time Thresholds in detail with examples. Moment.js is very easy to customize. In general, you should create a locale setting with your customizations.

The moment.relativeTimeThreshold() is used in duration. Humanize the display such that the duration is shown as a few seconds, a minute, an hour, etc. The number of seconds is predetermined and shown as of a few seconds ago; the same is true for minutes and hours. Using the relative time threshold method, you can modify the limit in seconds, minutes, hours, and days. 

Syntax:

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

To change those cutoffs, use moment.relativeTimeThreshold(unit, limit) where limit that is one of s, m, h, d, M:

  • ss a few seconds least numbers of seconds to be counted in seconds, minus 1. Must be set after setting the `s` unit or without setting the `s` unit.
  • s seconds least number of seconds to be considered a minute
  • m minutes least number of minutes to be considered an hour
  • h hours least number of hours to be considered a day
  • d days least number of days to be considered a month
  • w  weeks least number of weeks to be considered a month. Not used by default.
  • M months least number of months to be considered a year

Note: This will not work in the normal Node.js program because it requires an external moment.js library to be installed globally or in the project directory. For more details, please refer to this link.

Moment.js can be installed using the following command:

Installation of moment module:

npm install moment

Example 1: In this example, we will look at the default time as a few seconds, seconds, minutes, hours, days, and months by passing the respective parameter accordingly to the moment.relativeTimeThreshold() function in the javascript programming language.

Javascript




// Importing moment module
const moment = require('moment');
const a = moment.relativeTimeThreshold('ss');
console.log("
    Least number of seconds to be counted in seconds", a);
const b = moment.relativeTimeThreshold('s');
console.log("
    Least number of seconds to be considered as a minute", b);
const c = moment.relativeTimeThreshold('m');
console.log("
    Least number of minutes to be considered as a hour", c);
const d = moment.relativeTimeThreshold('h');
console.log("
    Least number of  hours to be considered as a day", d);
const e = moment.relativeTimeThreshold('d');
console.log("
    Least number of days to be considered as a month", e);
const f = moment.relativeTimeThreshold('M');
console.log("
    Least number of seconds to be considered as a minute", f);


Output:

Least number of seconds to be counted in seconds 44
Least number of seconds to be considered as a minute 45
Least number of minutes to be considered as a hour 45
Least number of  hours to be considered as a day 22
Least number of days to be considered as a month 26
Least number of seconds to be considered as a minute 11

Note: The minute threshold is changed from default 45 to 5, and the output for humanizing for 6 minutes is displayed as in an hour.

Example 2: In this example, we will change the Minute threshold from default 45 to 15, and the output for humanizing for 6 minutes is displayed as in 6 minutes using the moment.relativeTimeThreshold() with moment.duration().humanize() function and display humanize for set minutes is displayed as in set minutes. in javascript.

Javascript




// Importing moment module
const moment = require('moment');
moment.relativeTimeThreshold('m', 5);
const c = moment.duration(8, "minutes").humanize(true);
console.log(c)
moment.relativeTimeThreshold('m', 15);
c = moment.duration(8, "minutes").humanize(true);
console.log(c)


Output:

in an hour
in 8 minutes

Reference: https://momentjs.com/docs/#/customization/relative-time-threshold/



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads