8

我用moment.js人类可读的格式来格式化持续时间。
例如(d是一个Date对象):

moment(d).subtract("days", 3).from(d) // 返回 "3 days ago"

现在我想得到“2 周前”,但下面的代码返回以天为单位的持续时间

moment(d).subtract("weeks", 2).from(d) // 返回 "14 天前" i/o "2 周前"

我怎样才能得到“2周前” moment.js

4

6 回答 6

9

You can do this pretty easily with a custom callback function.

moment.relativeTime.dd = function (number) {
    // round to the closest number of weeks
    var weeks = Math.round(number / 7);
    if (number < 7) {
        // if less than a week, use days
        return number + " days";
    } else {
        // pluralize weeks
        return weeks + " week" + (weeks === 1 ? "" : "s"); 
    }
}

http://jsfiddle.net/timrwood/sWsXQ/

于 2012-08-03T23:57:02.127 回答
6

使用新的updateLocale().

Moment.js 已再次更新,因此首选方法如下:

moment.updateLocale('en', {

  relativeTime : {
    future: 'in %s',
    past:   '%s ago',
    s:      'a few seconds',
    m:      'a minute',
    mm:     '%d minutes',
    h:      'an hour',
    hh:     '%d hours',
    d:      'a day',
    dd:     function(number) {
              if (number < 7) {
                return number + ' days'; // Moment uses "d" when it's just 1 day.
              }
              else {
                var weeks = Math.round(number / 7);
                return weeks + ' ' + (weeks > 1 ? 'weeks' : 'week');
              }
            },
    M:      'a month',
    MM:     '%d months',
    y:      'a year',
    yy:     '%d years'
  }

});

感谢@timrwood@gahen的回答。

我正在努力更新 Moment,以便您可以仅覆盖单个 relativeTime对象,例如dd,而不必提供所有对象。

GitHub 问题在这里,所以如果你想做这样的事情,请给它一个大拇指:

moment.updateLocale('en', {

  relativeTime : {
    dd: function(number) {
          if (number < 7) {
            return number + ' days'; // Moment uses "d" when it's just 1 day.
          }
          else {
            var weeks = Math.round(number / 7);
            return weeks + ' ' + (weeks > 1 ? 'weeks' : 'week');
          }
        }
  }

});
于 2016-04-24T00:59:21.617 回答
5

我无法评论上一个答案,但我想留下一个更新的答案(基于timrwood的)

moment.locale('en', {
  relativeTime : {
    future: "in %s",
    past:   "%s ago",
    s:  "seconds",
    m:  "a minute",
    mm: "%d minutes",
    h:  "an hour",
    hh: "%d hours",
    d:  "a day",
    dd: function (number) {
      var weeks = Math.round(number / 7);
      if (number < 7) {
        // if less than a week, use days
        return number + " days";
      } else {
        // pluralize weeks
        return weeks + " week" + (weeks === 1 ? "" : "s"); 
      }
    },
    M:  "a month",
    MM: "%d months",
    y:  "a year",
    yy: "%d years"
  }
});

$window.moment.relativeTimeThreshold('d', 340); // if you want weeks instead of months, otherwise put a 28 or so.
于 2015-04-24T19:11:41.777 回答
2

改进@vinjenzo 的答案,当您需要几周内的日期差异时,您很可能会遇到返回的天数:

var aPastDate = moment().subtract(5,'months');
var aPastDay = moment().subtract(6,'days');
var now = moment();

moment.fn.durationInWeeks = function(fromDate, toDate) {

    var days    = toDate.diff(fromDate, 'days');    
    var weeks   = toDate.diff(fromDate, 'weeks');

    if (weeks === 0) {
        return days + ' ' + (days > 1 ? 'days' : 'day');
    } else {
        return weeks + ' ' + (Math.round(days / 7) > 1 ? 'weeks' : 'week');
    }

}

moment().durationInWeeks(aPastDate,now); // 21 weeks
moment().durationInWeeks(aPastDay,now);  // 6 days
于 2016-05-13T13:40:46.870 回答
1

文档列出了创建这些字符串的规则(在“从另一个时刻开始人性化时间”下)。虽然您可以通过更改来修改字符串moment.relativeTime,但这仅限于显示在数字周围的文本,即您不能将天更改为周。

您必须扩展moment.js或编写一些自定义代码来实现此功能。

于 2012-07-12T10:59:42.460 回答
1

分享并希望仍然有用:

var aPastDate = moment().subtract(5,'months');
var now = moment();


moment.fn.durationInWeeks = function (fromDate, toDate) {

   var weeks =  toDate.diff( fromDate, 'weeks' );
   return weeks + ' weeks ago'; //your own format

}

moment().durationInWeeks(aPastDate,now);

//"21 weeks ago"
于 2016-01-28T21:32:45.220 回答