如何获得持续的周Moment.js?(How to get duration in weeks wi

2019-06-26 06:06发布

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

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"

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

Answer 1:

您可以使用自定义的回调函数做到这一点很容易地。

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/



Answer 2:

使用新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了他们的答案。

我在作品中得到瞬间更新,以便您可以覆盖只是一个单一的 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');
          }
        }
  }

});


Answer 3:

我无法对前面的回答发表评论,但我想离开一个更新的答案(基于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.


Answer 4:

该文件列出了用于创建这些字符串规则(在“从另一个一刻时间实现人性化”)。 虽然可以通过更改修改字符串moment.relativeTime ,这仅限于围绕数字显示的文本,即你不能改变天至数周。

你将不得不延长moment.js或编写一些自定义代码来实现这个功能。



Answer 5:

分享,希望仍然是有用的:

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"


Answer 6:

提高对@ 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


文章来源: How to get duration in weeks with Moment.js?