我在一个网站中使用moment.js,在这个网站中,我必须显示不同时间段之间的持续时间。我得到毫秒的持续时间,除以3600 * 1000之后,我得到了十进制小时格式。我正在尝试使用moment.js的人性化方法,但是还不够精确。
这是库的示例行为。
moment.duration(0.5, 'hours').humanize(); // returns '30 minutes' OK
moment.duration(0.1, 'hours').humanize(); // returns '6 minutes' OK
moment.duration(0.8, 'hours').humanize(); // returns 'an hour' BAD
moment.duration(0.7, 'hours').humanize(); // returns '42 minutes' OK
moment.duration(0.72, 'hours').humanize(); // returns '43 minutes' OK
moment.duration(0.73, 'hours').humanize(); // returns '44 minutes' OK
moment.duration(0.74, 'hours').humanize(); // returns '44 minutes' OK
moment.duration(0.75, 'hours').humanize(); // returns 'an hour' BAD现在发生了什么,动量. is是如何解决的呢?
非常感谢。
发布于 2017-10-24 16:13:42
默认的分钟相对时间阈值是45分钟。
duration.humanize的阈值定义了一个单元何时被认为是一分钟、一小时等等。例如,默认情况下,超过45秒被认为是一分钟,超过22个小时被认为是一天,依此类推。要改变这些断口,请使用moment.relativeTimeThreshold(unit, limit),其中单元为ss、s、m、h、d、M。
如果您想要一个更高的临界点,请通过以下操作设置一个不同的值:
moment.relativeTimeThreshold('m', 60);
moment.relativeTimeThreshold('m', 60);
console.log([
moment.duration(0.5, 'hours').humanize(),
moment.duration(0.1, 'hours').humanize(),
moment.duration(0.8, 'hours').humanize(),
moment.duration(0.7, 'hours').humanize(),
moment.duration(0.72, 'hours').humanize(),
moment.duration(0.73, 'hours').humanize(),
moment.duration(0.74, 'hours').humanize(),
moment.duration(0.75, 'hours').humanize()
]);<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.19.1/moment.min.js"></script>
https://stackoverflow.com/questions/46915080
复制相似问题