【发布时间】:2015-03-22 07:41:07
【问题描述】:
我将在代码中解释我的问题。请看下面的代码
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;
}
但问题是month 一直在使用14。这是错误的。在 01-DEC-14 之后的下个月必须是 01-JAN-15、01-FEB-15 等等。如何在 for 循环中检查 DEC 并在 DEC 年后必须更改为 year+1
提前致谢
【问题讨论】:
标签: javascript jquery arrays if-statement for-loop