Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js change the fromNow time range

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',
  },
});
like image 283
Labithiotis Avatar asked Jul 04 '13 14:07

Labithiotis


People also ask

How do I add time to MomentJs?

To add time, pass the key of what time you want to add, and the amount you want to add. moment(). add(7, 'days'); There are some shorthand keys as well if you're into that whole brevity thing.

Is MomentJs deprecated?

MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week.


1 Answers

duration.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 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
  • M 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.

like image 159
EpokK Avatar answered Oct 24 '22 11:10

EpokK