簡體   English   中英

if條件基於jquery中for循環中的字符串

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

我將在代碼本身中解釋我的問題。 請看下面的代碼

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所有month都需要花費14 哪有錯 在14年12月1日之后,下個月必須是15年1月1日,15年2月1日,依此類推。 如何在循環中檢查DEC,並且DEC年之后必須更改為year + 1

提前致謝

您可以聲明變量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;
    }
}

第二種選擇是使用Date對象,並且每次將他的月份追加一個,如果您將append設置為第12號,它將自動轉到下一年。

使用下面的代碼,它將起作用。

 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