如何使用Moment.js在几周内获得持续时间?

时间:2012-07-12 09:00:50

标签: javascript momentjs

我使用moment.js格式化人类可读格式的持续时间 例如(dDate对象):

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

现在我想“2周前”,但下面的代码会返回持续时间

moment(d).subtract("weeks", 2).from(d) // returns "14 days ago" i/o "2 weeks ago"

如何使用moment.js获取“2周前”?

6 个答案:

答案 0 :(得分:9)

您可以使用自定义回调函数轻松完成此操作。

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/

答案 1 :(得分:5)

使用新的updateLocale()

Moment.js已再次更新,因此the preferred method如下:

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问题是here,所以如果您想要这样做,请竖起大拇指:

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');
          }
        }
  }

});

答案 2 :(得分:4)

我无法评论之前的答案,但我想留下更新的答案(基于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.

答案 3 :(得分:1)

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

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

答案 4 :(得分: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"

答案 5 :(得分:0)

改进@ 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