6

我可以更改 moment.js 中的时间范围吗fromNow(),所以小时范围是 60 秒到 59 分钟,其他人也不能(90 秒 - 45 分钟)。

ref: Moment.js 从现在开始的时间

是否有类似于如何更改语言的方法:

moment.lang('en', {
  relativeTime: {
    future: 'Due in %s',
    past: '%s ago',
    s: 'seconds',
    m: 'a minute',
    mm: '%d minutes',
    h: 'an hour',
    hh: '%d hours',
    d: 'a day',
    dd: '%d days',
    M: 'a month',
    MM: '%d months',
    y: 'a year',
    yy: '%d years',
  },
});
4

1 回答 1

4

duration.humanize具有定义何时将单位视为一分钟、一小时等的阈值。例如,默认情况下,超过 45 秒被认为是一分钟,超过 22 小时被认为是一天等等。

要更改这些截止值,请使用moment.relativeTimeThreshold(unit, limit)where limit 是s, m, h, d,之一M

  • s seconds 被认为是一分钟的最小秒数
  • m 分钟 被视为一小时的最少分钟数
  • h hours 一天的最少小时数
  • d days 被视为一个月的最少天数
  • M 月 被视为一年的最少月数

  // Retrieve existing thresholds
  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('s', 40);
  moment.relativeTimeThreshold('m', 40);
  moment.relativeTimeThreshold('h', 20);
  moment.relativeTimeThreshold('d', 25);
  moment.relativeTimeThreshold('M', 10);

注意:检索阈值是在2.8.1中添加的。

于 2016-02-14T16:23:01.147 回答