我試圖將日期設置爲今天的日期(不包括週末和英國公共假日)的7個工作日。設置日期從今天起7個工作日(不包括週末和公共假日)
- 我開始由默認日期設置爲今天的日期(todaysDate)+ 7天(todayPlusSevenDays)
- 然後我算todaysDate之間週末& todayPlusSevenDays 數。如果我發現任何我將它們添加到todayPlusSevenDays
- 然後我檢查公衆假期,如果我找到任何我也將它們添加
進行那些檢查我現在已經增加了額外的天,我的默認日期之後 - 我怎麼能還檢查的新範圍天包含週末或公衆假期?
例如,如果默認日期成爲週末或銀行假期,它也應該添加更多天(現在不會)。
這是到目前爲止我的代碼: 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');
}
});
}
可能重複[*添加工作日的使用JAV ascript *](http://stackoverflow.com/questions/40739059/add-working-days-using-javascript)。 – RobG