我用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
?
我用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
?
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");
}
}
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'
}
});
我正在努力更新 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');
}
}
}
});
我无法评论上一个答案,但我想留下一个更新的答案(基于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.
改进@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
文档列出了创建这些字符串的规则(在“从另一个时刻开始人性化时间”下)。虽然您可以通过更改来修改字符串moment.relativeTime
,但这仅限于显示在数字周围的文本,即您不能将天更改为周。
您必须扩展moment.js
或编写一些自定义代码来实现此功能。
分享并希望仍然有用:
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"