Moment.js 更改 fromNow 时间范围

Lab*_*tis 6 javascript momentjs

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

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

是否有类似于如何更改 lang 的内容:

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',
  },
});
Run Code Online (Sandbox Code Playgroud)

Epo*_*okK 4

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

要更改这些截止值,请使用limit 是, , , ,moment.relativeTimeThreshold(unit, limit)之一。smhdM

  • s 秒 被视为一分钟的最少秒数
  • m 分钟 被视为一小时的最少分钟数
  • h 小时 一天中考虑的最少小时数
  • d 天 被视为一个月的最少天数
  • 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);
Run Code Online (Sandbox Code Playgroud)

注意: 2.8.1中添加了检索阈值。