Permalink
6e99146 Jul 3, 2016
43 lines (33 sloc) 1.16 KB
title version signature
Relative Time Rounding
2.14.0
moment.relativeTimeRounding(); // getter 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.

var roundingDefault = moment.relativeTimeRounding();

// Round relative time evaluation down
moment.relativeTimeRounding(Math.floor);

moment.relativeTimeThreshold('s', 60);
moment.relativeTimeThreshold('m', 60);
moment.relativeTimeThreshold('h', 24);
moment.relativeTimeThreshold('d', 31);
moment.relativeTimeThreshold('M', 12);

var a = moment();
a.subtract({hours: 23, minutes: 59, seconds: 59});
a.toNow()  // == 'in 23 hours'  'Round down towards the nearest hour'

// back to default
moment.relativeTimeRounding(roundingDefault);

You can even choose to do no rounding at all:

var retainValue = function (value) {
    return value;
};
moment.relativeTimeRounding(retainValue);

var a = moment();
a.subtract({hours: 39});
a.toNow() // == 'in 1.625 days', 'Round down towards the nearest year'