Relative Time Rounding2.14.0+

  1. moment.relativeTimeRounding(); // getter
  2. moment.relativeTimeRounding(fn); // setter

duration.humanize rounds a possibly double value before supplying it to the relativeTime format string specified in the locale. To control the rounding you can use moment.relativeTimeRounding.

  1. var roundingDefault = moment.relativeTimeRounding();
  2. // Round relative time evaluation down
  3. moment.relativeTimeRounding(Math.floor);
  4. moment.relativeTimeThreshold('s', 60);
  5. moment.relativeTimeThreshold('m', 60);
  6. moment.relativeTimeThreshold('h', 24);
  7. moment.relativeTimeThreshold('d', 31);
  8. moment.relativeTimeThreshold('M', 12);
  9. var a = moment();
  10. a.subtract({hours: 23, minutes: 59, seconds: 59});
  11. a.toNow() // == 'in 23 hours' 'Round down towards the nearest hour'
  12. // back to default
  13. moment.relativeTimeRounding(roundingDefault);

You can even choose to do no rounding at all:

  1. var retainValue = function (value) {
  2. return value;
  3. };
  4. moment.relativeTimeRounding(retainValue);
  5. var a = moment();
  6. a.subtract({hours: 39});
  7. a.toNow() // == 'in 1.625 days', 'Round down towards the nearest year'