2015-10-06 110 views
1

我用Javascript編寫的函數有問題。我試圖在開始和結束之間獲得一組日期。獲取開始日期和結束日期之間的一個數組

功能:

function getDateArray(startDate, endDate) { 

    var dateArray = new Array(), 
    currentDate = new Date(startDate), 
    lastDay = new Date(endDate); 
    while (currentDate <= lastDay) { 
     if (!(currentDate.getUTCDay() === 0 || currentDate.getUTCDay() === 6)) { 
      //currentDate.toUTCString(); //This line is redundant 
      dateArray.push(currentDate); 
     } 
     currentDate.setDate(currentDate.getDate() + 1); 
    } 
    return dateArray; 
} 

每當我打電話有兩個日期這樣的功能:

開始日期= 2015年10月6日 和結束日期= 2015年10月12日

我得到了不想要的結果:

Tue Oct 13 2015 00:00:00 GMT+0200 (South Africa Standard Time) 
Tue Oct 13 2015 00:00:00 GMT+0200 (South Africa Standard Time) 
Tue Oct 13 2015 00:00:00 GMT+0200 (South Africa Standard Time) 
Tue Oct 13 2015 00:00:00 GMT+0200 (South Africa Standard Time) 
Tue Oct 13 2015 00:00:00 GMT+0200 (South Africa Standard Time) 
Tue Oct 13 2015 00:00:00 GMT+0200 (South Africa Standard Time) 
Tue Oct 13 2015 00:00:00 GMT+0200 (South Africa Standard Time) 

請如果有人可以highligh對我來說這裏有什麼不對?到那個

dateArray.push(currentDate); 

+0

'currentDate.toUTCString();'是一個無操作;你必須在某處分配結果。如果目標是推送字符串表單,則需要推送該值的返回值。 – ShadowRanger

回答

1

這是因爲你是推一個參考每次的currentdate(如日期是複雜的類型,它們是通過引用傳遞)

只需更換這

dateArray.push(new Date(currentDate)); 
+0

謝謝你。它正在工作。 – Memphis335

相關問題