【问题标题】:if condition based on a string in for loop in jqueryif条件基于jquery中for循环中的字符串
【发布时间】: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


    【解决方案1】:

    使用下面的代码就可以了。

    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 (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++) {
    
    if(dynmonths[i]=='JAN')
    {
    var str = parseInt(str)+parseInt(1);
    }
    var month = '01-' + dynmonths[i] + '-' + str;
        document.writeln(month);
    	document.write("<br />");
       
    }
    }
    &lt;body onload="ddd()"&gt;

    【讨论】:

      【解决方案2】:

      您可以声明变量 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 个月,它会自动转到下一年。

      【讨论】:

        猜你喜欢
        • 2014-05-21
        • 2013-12-19
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-09-12
        • 1970-01-01
        相关资源
        最近更新 更多