2014-02-28 46 views
0
function dateColumn() { 
    var startDate = new Date($("#anStartDate").val()); //the start date 
    var pmtPeriods = $("#anPaymentPeriods").val(); //number of months in this case 
    var dateData = new Array(pmtPeriods); 
    var i = 0; 
    while (i < pmtPeriods) { 
     startDate = startDate.add(i).month(); 
     dateData[i] = startDate; 
     i++ 
    } 
    alert(dateData); 
} 

假設我的開始日期是2014-01-01,並且我將2個月作爲pmtPeriods。用[2014-02-01, 2014-02-01]填充我的數組。如果我將pmtPeriods作爲3,並且開始日期相同,則結果爲[2014-03-01, 2014-03-01, 2014-03-01]。這是錯誤的。使用循環填充數組,使用月份

隨着pmtPeriods 2,我想結果是:

[2014-02-01, 2014-03-01] 

代替

[2014-02-01, 2014-02-01] 
+1

什麼是你的問題? – forgivenson

+0

「與pmtPeriods 2我想結果:[2014-02-01,2014-03-01]」? – MrProgram

回答

3

您要添加的startDate到dateData陣列的3倍。這只是添加一個引用到你的循環中得到更新的startDate值。更改:

dateData[i] = startDate; 

dateData[i] = new Date(startDate); 
+0

哦......有時你覺得你如此親密,但無法解決它。這解決了我的問題(責備整天工作;-))。謝謝! – MrProgram