简体   繁体   English

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

[英]if condition based on a string in for loop in jquery

I will explain my question in the code itself. 我将在代码本身中解释我的问题。 Please see the below code 请看下面的代码

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;
}

But the problem is that month is taking 14 for all the months. 但是问题在于,该month所有month都需要花费14 Which is wrong. 哪有错 After 01-DEC-14 the next month must be 01-JAN-15, 01-FEB-15 and so on. 在14年12月1日之后,下个月必须是15年1月1日,15年2月1日,依此类推。 How to check DEC in for loop and after DEC year must change to year+1 如何在循环中检查DEC,并且DEC年之后必须更改为year + 1

Thanks in advance 提前致谢

You can declare variable bool = false and check if you on DEC change it to true (or use counter from more then one year): 您可以声明变量bool = false并检查您是否在DEC上将其更改为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;
    }
}

second option is to use Date object and append his month by one each time, if you set append to month number 12 it automatic go to the next year. 第二种选择是使用Date对象,并且每次将他的月份追加一个,如果您将append设置为第12号,它将自动转到下一年。

use below code it will work. 使用下面的代码,它将起作用。

 function ddd() { 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 (ie APR in this case) to current month (ie 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 ie between (01-APR-14 to 01-MAR-15) for (var i = 0, length = dynmonths.length; i < length; i++) { if(dynmonths[i]=='JAN') { var str = parseInt(str)+parseInt(1); } var month = '01-' + dynmonths[i] + '-' + str; document.writeln(month); document.write("<br />"); } } 
 <body onload="ddd()"> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM