moment.relativeTimeThreshold(unit); // getter
moment.relativeTimeThreshold(unit, limit); // setter
duration.humanize
具有阈值,这些阈值定义何时将一个单位视为一分钟、一小时等。
例如,默认情况下,超过45秒会被视为一分钟,超过22小时会被视为一天,依此类推。
要更改这些截止值,则使用 moment.relativeTimeThreshold(unit, limit)
,其中 unit 是 ss
、s
、m
、h
、d
、M
之一。
单位 | 含义 | 用法 |
---|---|---|
ss | 几秒钟 | 被认为是秒钟的最少秒数。必须在设置 `s` 单位之后设置,或者不设置 `s` 单位。 |
s | 秒钟 | 被认为是一分钟的最少秒数。 |
m | 分钟 | 被认为是一小时的最少分钟数。 |
h | 小时 | 被认为是一天的最少小时数。 |
d | 天 | 被认为是一个月的最少天数。 |
M | 月份 | 被认为是一年的最少月份数。 |
// 检索现有的阈值。
moment.relativeTimeThreshold('ss'); // 44
moment.relativeTimeThreshold('s'); // 45
moment.relativeTimeThreshold('m'); // 45
moment.relativeTimeThreshold('h'); // 22
moment.relativeTimeThreshold('d'); // 26
moment.relativeTimeThreshold('M'); // 11
// 设置新的阈值。
moment.relativeTimeThreshold('ss', 3);
moment.relativeTimeThreshold('s', 40);
moment.relativeTimeThreshold('m', 40);
moment.relativeTimeThreshold('h', 20);
moment.relativeTimeThreshold('d', 25);
moment.relativeTimeThreshold('M', 10);
注意:检索阈值新增于 2.8.1。
注意:检索与设置 ss
阈值新增于 2.18.0。
moment.relativeTimeThreshold(unit); // getter
moment.relativeTimeThreshold(unit, limit); // setter
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 unit is one of ss
, s
, m
, h
, d
, M
.
unit | meaning | usage |
---|---|---|
ss | a few seconds | least number of seconds to be considered seconds. Must be set after setting the `s` unit or without setting the `s` unit. |
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('ss'); // 44
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('ss', 3);
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.
Note: Retrieving and setting ss
threshold was added in 2.18.0.