2016-10-03 117 views
4

我正在構建一個時間表函數,爲此我必須在集合中將明智的數據插入數據庫。在javascript中獲取從開始日期到結束日期的幾周

所以我創建一個數組,其中包含從start_date到end_date的星期。

1st推入一個數組: start_date =聲明日期(如果聲明日期是星期日,那麼它認爲星期一來的日期); end_date =週六的日期

第2到第n推入數組: start_date =星期一的日期;上週六END_DATE =日期,或者如果一週

var start = new Date("09/30/2016"); 
var end = new Date("11/2/2016"); 

var count = 0; 
var sDate; 
var eDate; 
var dateArr = []; 

while(start <= end){ 
    if (start.getDay() == 0){ 
     count = 0; 
    }else { 
     if(count == 0){ 
      sDate = start; 
      count = 1 
     }else { 
      count = 1; 
     } 

     if(start.getDay() == 6 || start == end){ 
      count = 1 
      eDate = start; 
     }else { 
      count = 1; 
     } 

     if(sDate && eDate){ 
      sDate = new Date(sDate) 
      eDate = new Date(eDate) 
      dateArr.push({'startDate': sDate, 'endDate': eDate}); 
      sDate = undefined; 
      eDate = undefined; 
     } 
    } 

    var newDate = start.setDate(start.getDate() + 1); 
    start = new Date(newDate); 
} 

但結果即時得到內的是這個

[{ 
    'startDate':Sat Oct 01 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 01 2016 00:00:00 GMT+0530 (IST), 
}, 
{ 
    'startDate':Tue Oct 04 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 08 2016 00:00:00 GMT+0530 (IST), 
}, 
{ 
    'startDate':Tue Oct 11 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 15 2016 00:00:00 GMT+0530 (IST), 
}, 
{ 
    'startDate':Tue Oct 18 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 22 2016 00:00:00 GMT+0530 (IST), 
}, 
{ 
    'startDate':Tue Oct 25 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 29 2016 00:00:00 GMT+0530 (IST), 
}] 

編輯宣告結束日期:

預期結果:

[{ 
    'startDate':Fri Sep 30 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 01 2016 00:00:00 GMT+0530 (IST), 
}, 
{ 
    'startDate':Mon Oct 03 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 08 2016 00:00:00 GMT+0530 (IST), 
}, 
{ 
    'startDate':Mon Oct 10 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 15 2016 00:00:00 GMT+0530 (IST), 
}, 
{ 
    'startDate':Mon Oct 17 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 22 2016 00:00:00 GMT+0530 (IST), 
}, 
{ 
    'startDate':Mon Oct 24 2016 00:00:00 GMT+0530 (IST), 
    'endDate':Sat Oct 29 2016 00:00:00 GMT+0530 (IST), 
}, 
{ 
    'startDate':Mon Oct 31 2016 00:00:00 GMT+0530 (IST) 
    'endDate':Wed Nov 02 2016 00:00:00 GMT+0530 (IST), 
}] 
+1

查看[momentsjs](http://momentjs.com/) – miraculixx

+0

@miraculixx在推入數組之前,如果console.log顯示星期一,則開始日期。 我也嘗試過使用moments.js。開始日期仍顯示爲下一個日期 – Monasha

+0

您需要什麼結果(日期之間的星期數)? –

回答

2

檢查一下。我修復了一些邏輯和obj引用錯誤。

var start = new Date(Date.UTC(2016, 09, 30, 0, 0, 0)); 
var end = new Date(Date.UTC(2016, 11, 02, 0, 0, 0)); 
var sDate; 
var eDate; 
var dateArr = []; 

while(start <= end){ 
    if (start.getDay() == 1 || (dateArr.length == 0 && !sDate)){ 
    sDate = new Date(start.getTime()); 
    } 

    if ((sDate && start.getDay() == 0) || start.getTime() == end.getTime()){ 
     eDate = new Date(start.getTime()); 
    } 

    if(sDate && eDate){ 
    dateArr.push({'startDate': sDate, 'endDate': eDate}); 
    sDate = undefined; 
    eDate = undefined; 
    } 

    start.setDate(start.getDate() + 1); 
} 

console.log(dateArr); 

https://jsfiddle.net/c58zde4b/6/

顯示的日期可能因當地時區設置。

+0

在這個數組中的第一個對象是星期一,但在我的預期結果是星期五。所有其他對象的開始日期都是星期天。加上對象的數量是1減少 – Monasha

+0

請參閱更新的答案。 - 注意:星期日爲0,星期一爲1,依此類推。 - 如果日期不匹配一個小的偏移量,使您正在查看正確的時區。 –

+0

我已經對您的答案進行了一些更改,只是對其進行了審查。我將其標記爲答案 – Monasha

相關問題