javascript 如何使用 Moment.js 获得以周为单位的持续时间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11448340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get duration in weeks with Moment.js?
提问by Michael
I use moment.js
to format durations in human readable format.
For example (d
is a Date
object):
我moment.js
用来格式化人类可读格式的持续时间。
例如(d
是一个Date
对象):
moment(d).subtract("days", 3).from(d) // returns "3 days ago"
Now I would like to get "2 weeks ago" but the code below returns the durations in days
现在我想得到“2周前”,但下面的代码以天为单位返回持续时间
moment(d).subtract("weeks", 2).from(d) // returns "14 days ago" i/o "2 weeks ago"
How can I get "2 weeks ago" with moment.js
?
我怎样才能得到“2周前” moment.js
?
回答by timrwood
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");
}
}
回答by Joshua Pinter
Use the new updateLocale()
.
使用新的updateLocale()
.
Moment.js has been updated again so the preferred methodis the following:
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'
}
});
Thanks to @timrwoodand @gahenfor their answers.
I'm in the works to get Moment updated so that you can override just a singlerelativeTime
object, like dd
, instead of having to provide all of the objects.
我正在努力更新 Moment,以便您可以仅覆盖单个relativeTime
对象,例如dd
,而不必提供所有对象。
The GitHub Issue is here, so give it a thumbs up if you'd like to do something like this instead:
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');
}
}
}
});
回答by gahen
I can't comment on the previous answer but I wanted to leave an updated answer (based on timrwood's)
我无法对之前的答案发表评论,但我想留下一个更新的答案(基于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.
回答by vinjenzo
sharing and hoping still useful:
分享并希望仍然有用:
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"
回答by Lars Kotthoff
The documentationlists the rules for creating those strings (under "Humanize time from another moment"). While you can modify the strings by changing moment.relativeTime
, this is limited to the text that is displayed around the number, i.e. you can't change days to weeks.
该文档列出了创建这些字符串的规则(在“从另一个时刻将时间人性化”下)。虽然您可以通过更改来修改字符串moment.relativeTime
,但这仅限于显示在数字周围的文本,即您不能将天更改为周。
You'll have to extend moment.js
or write some custom code to implement this functionality.
您必须扩展moment.js
或编写一些自定义代码来实现此功能。
回答by cenk
improving on @vinjenzo's answer, when you need date diff in weeks, you will most likely encounter days returned:
改进@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