2017-08-12 95 views
1

我正在處理一個問題,我有一個開始日期和天數,所以當一個員工要求離開讓我們說20天,所以程序是添加日期到該日期,沒有包括週末和假期,以獲得此人應該恢復工作的日期。這是迄今爲止我一直在努力的。但我沒有得到真正的最後日期,員工離職日期計算在Javascript

//功能整理出假期

function isHoliday(dt, arr){ 
    var bln = false; 
     for (var i = 0; i < arr.length; i++) { 
      if (compare(dt, arr[i])) { //If days are not holidays 
       bln = true; 
       break; 
        } 
     } 
      return bln; 
    } 

//下面的功能可以幫助假期比較程序

function compare(dt1, dt2){ 
     var equal = false; 
      if(dt1.getDate() == dt2.getDate() && dt1.getMonth() == dt2.getMonth() && dt1.getFullYear() == dt2.getFullYear()) { 
      equal = true; 
     } 
     return equal; 
    } 
現在

在我的主要功能我嘗試使用整理假期和週末,並添加幾天到開始日期,以便我可以存檔結束日期。我認爲我在這個代碼的某個地方出了問題,現在正在堆棧中。

function returnfinaldate() 
    { 
    var holiday = []; 

    var cy = new Date().getFullYear(); 
    holiday[0] = new Date(cy, 1, 01); 
    holiday[1] = new Date(cy, 1, 26); 
    holiday[2] = new Date(cy, 2, 16); 
    holiday[3] = new Date(cy, 3, 08); 
    holiday[4] = new Date(cy, 3, 20); 
    holiday[5] = new Date(cy, 4, 14); 
    holiday[6] = new Date(cy, 4, 16); 
    holiday[7] = new Date(cy, 4, 17); 
    holiday[8] = new Date(cy, 5, 01); 
    holiday[9] = new Date(cy, 5, 14); 
    holiday[10] = new Date(cy, 6, 03); 
    holiday[11] = new Date(cy, 6, 09); 
    holiday[12] = new Date(cy, 6, 21); 
    holiday[13] = new Date(cy, 6, 21); 
    holiday[14] = new Date(cy, 6, 26); 
    holiday[15] = new Date(cy, 9, 02); 
    holiday[16] = new Date(cy, 9, 22); 
    holiday[17] = new Date(cy, 10, 09); 
    holiday[18] = new Date(cy, 12, 21); 
    holiday[19] = new Date(cy, 12, 25); 
    holiday[20] = new Date(cy, 12, 26); 

    var startDate = new Date(); 
    var endDate = new Date(); 

    startDate='8/1/2017'; 

    noOfDaysToAdd = 7, count = 0; 

    while (count < noOfDaysToAdd) { 
     endDate.setDate(endDate.getDate()+1) 
     // Date.getDay() gives weekday starting from 0(Sunday) to 
     // 6(Saturday) 
     if (endDate.getDay() != 0 && endDate.getDay() != 6 && !isHoliday(endDate, holiday)) { 
      count++; 
     } 
    } 

    return endDate; 
    } 

最後的日期變得錯了。這裏需要一些幫助。

+0

你'endDate'是基於當前日期不'startDate' – charlietfl

回答

-1

這裏使用起始日期以天計算的起點,否則你的邏輯似乎罰款

function returnfinaldate() { 
      var holiday = []; 
      holiday[0] = new Date(2017, 9, 5);// holiday 1 
      holiday[1] = new Date(2017, 9, 6);//holiday 2 


      var startDate = new Date(); 
      var endDate = new Date(); 

      startDate = new Date(2017, 7, 1);// '8/1/2017'; 

      noOfDaysToAdd = 7, count = 0; 
      endDate = startDate; 
     //use below code to include start date in count else comment below code 
      // if (endDate.getDay() != 0 && endDate.getDay() != 6 && !isHoliday(endDate, holiday)) { 
      // count++; 
      // } 
      while (count < noOfDaysToAdd) { 
       endDate.setDate(endDate.getDate() + 1) 
       // Date.getDay() gives weekday starting from 0(Sunday) to 
       // 6(Saturday) 
       if (endDate.getDay() != 0 && endDate.getDay() != 6 && !isHoliday(endDate, holiday)) { 
        count++; 
       } 
      } 

      return endDate; 
     } 
+0

感謝名單先生約傑什的字符串版本。你剛剛救了我的命。它確實工作得很好 – Bels

+0

我剛剛實現了初始化endDate = startDate;給出endDate額外的月份 – Bels

+0

當創建開始日期從0開始的月份,如8月7日,如果這是正確的,那麼請提供我的例子,我會研究它 – Yogesh