moment.relativeTimeRounding(); // getter
moment.relativeTimeRounding(fn); // setter
duration.humanize
在将其提供给语言环境中指定的 relativeTime 格式字符串之前,会舍入一个可能为双精度的值。
要控制舍入,则可以使用 moment.relativeTimeRounding
。
var roundingDefault = moment.relativeTimeRounding();
// 向下舍入相对时间。
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() // == '23 小时内' '向下舍入到最近的小时'
// 回退到默认值。
moment.relativeTimeRounding(roundingDefault);
甚至可以选择完全不舍入:
var retainValue = function (value) {
return value;
};
moment.relativeTimeRounding(retainValue);
var a = moment();
a.subtract({hours: 39});
a.toNow() // == '1.625 天内', '向下舍入到最近的年份'
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'