Javascript 基于jquery中for循环中的字符串的if条件

Javascript 基于jquery中for循环中的字符串的if条件,javascript,jquery,arrays,if-statement,for-loop,Javascript,Jquery,Arrays,If Statement,For Loop,我将在代码中解释我的问题。请参阅下面的代码 var monthNames = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"]; var ctdate = (new Date()).getMonth() + 1;// getting current month var str=new Date().getFullYear()+''; str= str.match(/\d{2}$/);//curren

我将在代码中解释我的问题。请参阅下面的代码

var monthNames = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var ctdate = (new Date()).getMonth() + 1;// getting current month
var str=new Date().getFullYear()+'';
str= str.match(/\d{2}$/);//current year is 15(as this year is 2015)
var strprev= str-1;//previous year is 14
var dynmonths = new Array();
dynmonths = monthNames.slice(ctdate).concat(monthNames.slice(0, ctdate));

//here the output comes for last 12 months starting from currentmonth-12 (i.e APR in this case) to current month (i.e MAR)
//dynmonths = ["APR","MAY","JUN","JUL","AUG","SEP","AUG","SEP","OCT","NOV","DEC","JAN","FEB","MAR"];

//I am rotating dynmonths in a for loop to get full dates i.e between (01-APR-14 to 01-MAR-15)

for (var i = 0, length = dynmonths.length; i < length; i++) {
var month = '01-' + dynmonths[i] + '-' + strcurrent;
}
但问题是,这个月花了14个月的时间。这是错误的。2014年12月1日之后的下一个月必须是2015年1月1日、2015年2月1日,依此类推。如何为循环签入12月以及12月之后的年份必须更改为+1年


提前感谢

您可以声明变量bool=false,并检查您是否在12月将其更改为true或使用一年以上的计数器:

 var monthNames = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
var ctdate = (new Date()).getMonth() + 1;// getting current month
var str=new Date().getFullYear()+'';
str= str.match(/\d{2}$/);//current year is 15(as this year is 2015)
var strprev= str-1;//previous year is 14
var dynmonths = new Array();
dynmonths = monthNames.slice(ctdate).concat(monthNames.slice(0, ctdate));

//here the output comes for last 12 months starting from currentmonth-12 (i.e APR in this case) to current month (i.e MAR)
//dynmonths = ["APR","MAY","JUN","JUL","AUG","SEP","AUG","SEP","OCT","NOV","DEC","JAN","FEB","MAR"];

//I am rotating dynmonths in a for loop to get full dates i.e between (01-APR-14 to 01-MAR-15)

var isPassYear = false;

for (var i = 0, length = dynmonths.length; i < length; i++) {
    var month;
    if (isPassYear)
        //do something
    else
        month  = '01-' + dynmonths[i] + '-' + strcurrent;

    if (monthNames[11] == dynmonths[i]) {
        isPassYear = true;
    }
}

第二个选项是使用Date对象,每次都将其月份追加一个,如果将“追加”设置为第12个月,则自动转到下一年。

使用下面的代码,它将起作用

功能ddd { var monthNames=[一月、二月、三月、四月、五月、六月、七月、八月、九月、十月、十一月、十二月]; var ctdate=new Date.getMonth+1;//获取当前月份 var str=新日期。getFullYear+; str=str.match/\d{2}$//;//本年度为15,本年度为2015年 var strprev=str-1;//上一年为14 var dynmonths=新阵列; dynmonths=monthNames.slicectdate.concatmonthNames.slice0,ctdate; //这里的输出是从当前月份-12(本例中为4月)到当前月份(即3月)的最后12个月的输出 //动态月=[四月、五月、六月、七月、八月、九月、八月、九月、十月、十一月、十二月、一月、二月、三月]; //我在for循环中旋转dynmons以获得完整日期,即2014年4月1日至2015年3月1日 对于变量i=0,length=dynmonths.length;i