Javascript JS:获取从特定日期到今天的所有月份(也使用moment.JS)
假设我有一个特定的日期,比如:Javascript JS:获取从特定日期到今天的所有月份(也使用moment.JS),javascript,date,momentjs,Javascript,Date,Momentjs,假设我有一个特定的日期,比如:21-03-2013,我想得到我使用的月份:时刻(“04-03-2013”,“DD-MM-YYYY”)。格式(“MMMM”)这让我有了前进的动力 现在,对于今天的日期,我使用moment().format('MM')这给了我十月的时间 如何在名称中获取两个月之间的所有月份?此函数获取格式为“DD-MM-YYYY”的字符串,并返回一个数组,其中包含从该日期到当前日期的所有月份 function getMonths(startDate){ var startM
21-03-2013
,我想得到我使用的月份:时刻(“04-03-2013”,“DD-MM-YYYY”)。格式(“MMMM”)代码>这让我有了前进的动力
现在,对于今天的日期,我使用moment().format('MM')代码>这给了我十月的时间
如何在名称中获取两个月之间的所有月份?此函数获取格式为“DD-MM-YYYY”的字符串,并返回一个数组,其中包含从该日期到当前日期的所有月份
function getMonths(startDate){
var startMonth = parseInt(startDate.split('-')[1], 10),
endMonth = parseInt(moment().format('M'), 10),
monthArray = [];
if( startMonth < 1 ) return [];
for( var i = startMonth; i != endMonth; i++ ){
if( i > 12 ) i = 1;
monthArray.push( moment(i, "M").format("MMMM") );
}
monthArray.push(moment().format('MMMM'));
return monthArray;
}
getMonths("04-03-2013");
函数getMonths(开始日期){
var startMonth=parseInt(startDate.split('-')[1],10),
endMonth=parseInt(矩().format('M'),10),
蒙塔雷=[];
如果(开始月<1)返回[];
for(变量i=startMonth;i!=endMonth;i++){
如果(i>12)i=1;
推(矩(i,“M”)格式(“MMMM”);
}
monthArray.push(矩().format('MMMM');
返回蒙塔雷;
}
获取月数(“04-03-2013”);
此函数获取格式为“DD-MM-YYYY”的字符串,并返回一个数组,其中包含从该日期到当前日期的所有月份
function getMonths(startDate){
var startMonth = parseInt(startDate.split('-')[1], 10),
endMonth = parseInt(moment().format('M'), 10),
monthArray = [];
if( startMonth < 1 ) return [];
for( var i = startMonth; i != endMonth; i++ ){
if( i > 12 ) i = 1;
monthArray.push( moment(i, "M").format("MMMM") );
}
monthArray.push(moment().format('MMMM'));
return monthArray;
}
getMonths("04-03-2013");
函数getMonths(开始日期){
var startMonth=parseInt(startDate.split('-')[1],10),
endMonth=parseInt(矩().format('M'),10),
蒙塔雷=[];
如果(开始月<1)返回[];
for(变量i=startMonth;i!=endMonth;i++){
如果(i>12)i=1;
推(矩(i,“M”)格式(“MMMM”);
}
monthArray.push(矩().format('MMMM');
返回蒙塔雷;
}
获取月数(“04-03-2013”);
如有必要,这也适用于更长的期限(>1年)
function getDates(startDate /*moment.js date object*/) {
nowNormalized = moment().startOf("month"), /* the first of current month */
startDateNormalized = startDate.clone().startOf("month").add("M", 1), /* the first of startDate + 1 Month - as it was asked for the months in between startDate and now */
months = [];
/* .isBefore() as it was asked for the months in between startDate and now */
while (startDateNormalized.isBefore(nowNormalized)) {
months.push(startDateNormalized.format("MMMM"));
startDateNormalized.add("M", 1);
}
return months;
}
更新
正如Matt在评论中所建议的那样,我现在使用.clone()
和.startOf(“月”)
而不是自己创建规范化克隆如果必要的话,这也将使用更长的时间(>1年)
function getDates(startDate /*moment.js date object*/) {
nowNormalized = moment().startOf("month"), /* the first of current month */
startDateNormalized = startDate.clone().startOf("month").add("M", 1), /* the first of startDate + 1 Month - as it was asked for the months in between startDate and now */
months = [];
/* .isBefore() as it was asked for the months in between startDate and now */
while (startDateNormalized.isBefore(nowNormalized)) {
months.push(startDateNormalized.format("MMMM"));
startDateNormalized.add("M", 1);
}
return months;
}
更新
正如Matt在评论中所建议的,我现在使用.clone()
和.startOf(“月”)
而不是自己创建规范化克隆在使用时始终指定基数-在这种情况下应该是parseInt(…,10)
。否则,像01-08-2013
或01-09-2013
这样的日期可能会导致错误的startMonth
在使用时始终指定基数-在这种情况下,应为parseInt(…,10)
。否则,类似于01-08-2013
或01-09-2013
的日期可能会导致错误的startMonth
正确答案。但是可能还需要检查isame
,或者使用也可以使用.clone().startOf('month')
来构建nownnormalized
和startDateNormalized
,但这只是一个小优化。@MattJohnson感谢.clone()
和startOf()的提示
-我还没有听说过。这是我使用moment.js的第一步:)回答得好。但是可能还需要检查isame
,或者使用也可以使用.clone().startOf('month')
来构建nownnormalized
和startDateNormalized
,但这只是一个小优化。@MattJohnson感谢.clone()
和startOf()的提示
-我还没有听说过。以下是我使用moment.js的第一步:)