2017-05-03 82 views
1

我試圖將日期設置爲今天的日期(不包括週末和英國公共假日)的7個工作日。設置日期從今天起7個工作日(不包括週末和公共假日)

  1. 我開始由默認日期設置爲今天的日期(todaysDate)+ 7天(todayPlusSevenDays)
  2. 然後我算todaysDate之間週末& todayPlusSevenDays 數。如果我發現任何我將它們添加到todayPlusSevenDays
  3. 然後我檢查公衆假期,如果我找到任何我也將它們添加

進行那些檢查我現在已經增加了額外的天,我的默認日期之後 - 我怎麼能還檢查的新範圍天包含週末或公衆假期?

例如,如果默認日期成爲週末或銀行假期,它也應該添加更多天(現在不會)。

這是到目前爲止我的代碼: https://jsfiddle.net/7yxna052/

function prepopulateDropdown() { 
    var todaysDate = new Date(), 
     tempNewDate = new Date(), 
     todayPlusSevenDays, 
     numberOfWeekends, 
     todayPlusSevenDaysPlusWeekends, 
     currentHour = todaysDate.getHours(), 
     holidayCount = 0, 
     weekendDayCount = 0, 
     ukHolidays = ['2017-05-12','2017-05-29','2017-08-28','2017-12-25','2017-12-26']; 

    // check if current time <or> 6pm GMT 
    function setDefaultdDate(){ 
    if(currentHour >= 18){ 
      todayPlusSevenDays = new Date(tempNewDate.setDate(tempNewDate.getDate() + 7)); 
     } 
     else{ 
      todayPlusSevenDays = new Date(tempNewDate.setDate(tempNewDate.getDate() + 6)); 
     } 
    } 
    setDefaultdDate(); 

    // Weekend day count 
    function calculateWeekendDays(startDate, endDate){ 
     while(startDate < endDate){ 
      startDate.setDate(startDate.getDate() + 1); 
      if(startDate.getDay() === 0 || startDate.getDay() == 6){ 
       ++weekendDayCount ; 
      } 
     } 
     return weekendDayCount; 
    } 
    calculateWeekendDays(todaysDate, todayPlusSevenDays); 

    todayPlusSevenDaysPlusWeekends = new Date(tempNewDate.setDate(tempNewDate.getDate() + weekendDayCount)); 



    // count UK bank holidays within todayPlusSevenDays 
    function calculateBankHolidays(startDate, endDate){ 
     startDate.setHours(0,0,0,0); 
     endDate.setHours(0,0,0,0); 

     for(i=0; i < ukHolidays.length; i++){ 
     ukHolidaysFormated = new Date(ukHolidays[i]).setHours(0,0,0,0); 
     d = new Date(ukHolidays[i]).getDay(); 

     if (ukHolidaysFormated >= startDate && ukHolidaysFormated <= endDate && !(d == 0 || d == 6)) { 
      holidayCount++; 
     } 
     } 
     return holidayCount; 
    } 
    calculateBankHolidays(todaysDate, todayPlusSevenDaysPlusWeekends); 

    todayPlusSevenDaysPlusWeekends = new Date(todayPlusSevenDaysPlusWeekends.setDate(todayPlusSevenDaysPlusWeekends.getDate() + holidayCount)); 


    // set date to prepopulate 
    var today = new Date(); 
    var year = todayPlusSevenDaysPlusWeekends.getFullYear(); 
    var month = '0' + (todayPlusSevenDaysPlusWeekends.getMonth() + 1); 
    var day = todayPlusSevenDaysPlusWeekends.getDate(); 

    $('.slctDay option').each(function(){ 
     if($(this).val() == day){ 
      $(this).attr('selected','selected'); 
     } 
    }); 
    $('.slctMonth option').each(function(){ 
     if($(this).val() == month){ 
      $(this).attr('selected','selected'); 
     } 
    }); 
    $('.slctYear option').each(function(){ 
     if($(this).val() == year){ 
      $(this).attr('selected','selected'); 
     } 
    }); 
} 
+0

可能重複[*添加工作日的使用JAV ascript *](http://stackoverflow.com/questions/40739059/add-working-days-using-javascript)。 – RobG

回答

2

這裏是@andi正在討論的一個例子。我將它作爲計算器對象。

var calculator = { 
 
    workDaysAdded: 0, 
 
    ukHolidays: ['2017-05-12','2017-05-29','2017-08-28','2017-12-25','2017-12-26'], 
 
    startDate: null, 
 
    curDate: null, 
 

 
    addWorkDay: function() { 
 
     this.curDate.setDate(this.curDate.getDate() + 1); 
 
     if(this.ukHolidays.indexOf(this.formatDate(this.curDate)) === -1 && this.curDate.getDay() !== 0 && this.curDate.getDay() !== 6) { 
 
      this.workDaysAdded++; 
 
     } 
 
    }, 
 

 
    formatDate: function(date) { 
 
     var day = date.getDate(), 
 
      month = date.getMonth() + 1; 
 

 
     month = month > 9 ? month : '0' + month; 
 
     day = day > 9 ? day : '0' + day; 
 
     return date.getFullYear() + '-' + month + '-' + day; 
 
    }, 
 

 
    getNewWorkDay: function(daysToAdd) { 
 
     this.startDate = new Date(); 
 
     this.curDate = new Date(); 
 
     this.workDaysAdded = 0; 
 
     
 
     while(this.workDaysAdded < daysToAdd) { 
 
      this.addWorkDay(); 
 
     } 
 
     return this.curDate; 
 
    } 
 
} 
 

 
var newWorkDay7 = calculator.getNewWorkDay(7); 
 
var newWorkDay9 = calculator.getNewWorkDay(9); 
 
var newWorkDay14 = calculator.getNewWorkDay(14); 
 
console.log(newWorkDay7); 
 
console.log(newWorkDay9); 
 
console.log(newWorkDay14);

+0

非常感謝這個例子。它像一個魅力。 – John

+0

正在做更多的測試,看起來像有一個錯誤。如果'curDate'是星期六或星期日,'newWorkDay'短於一天。 – John

+0

嗯,你可以再次嘗試代碼。我做了一個小小的改動,並增加了兩個額外的'console.logs'作爲它的工作範例。 – gforce301

2

入住一天是一天的時間,而不是7天的範圍內。

首先將默認日期設置爲今天的日期。然後,檢查將來的某一天。如果那一天是工作日,那麼將workingDay計數器增加1.如果不是,則循環到第二天。當你的工作日計數器達到7時,那就是你需要的日期。

+0

你對JS的知識看起來不錯,所以我假設你不需要爲此看到特定的JS。看起來你只需要將底層邏輯重定向到這個函數。但是如果您對如何編碼有任何疑問,請詢問。 – andi

+0

它確實有道理。非常感謝你的幫助。 – John

0

如果你只想增加7天,不包括節假日和週末(假設星期六和星期日),那麼你可以從開始到結束步驟,每天測試你去:

var ukHolidays = ['2017-05-12','2017-05-29','2017-08-28','2017-12-25','2017-12-26']; 
 

 
// Return date string in format YYYY-MM-DD 
 
function getISODate(date){ 
 
    function z(n) {return (n<10?'0':'')+n} 
 
    return date.getFullYear() + '-' + z(date.getMonth() + 1) + '-' + z(date.getDate()); 
 
} 
 

 
// Modifies date by adding 7 days, excluding sat, sun and UK holidays 
 
function add7WorkingDays(date) { 
 
    for (var i=7; i; i--) { 
 
    // Add a day 
 
    date.setDate(date.getDate() + 1); 
 

 
    // If a weekend or holiday, keep adding until not 
 
    while(!(date.getDay()%6) || ukHolidays.indexOf(getISODate(date)) != -1) { 
 
     date.setDate(date.getDate() + 1); 
 
    } 
 
    } 
 
    return date; 
 
} 
 

 
// Add 7 working days to today 
 
var d = new Date(); 
 
console.log('Add 7 working days to ' + d.toString() + 
 
      '\nResult: ' + add7WorkingDays(d).toString());

相關問題