我正在尝试将日期设定为从今天起的7个工作日(不包括周末和英国公众假期)。
执行这些检查后,我现在已经将额外的天数添加到我的默认日期 - 我怎样才能检查新的天数是否包含周末或公众假期?
例如,如果默认日期变为周末或银行假日,它还应该添加更多天数(现在它不会)。
到目前为止,这是我的代码: 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 :(得分:3)
一次检查一天而不是7天范围。
首先将默认日期设置为今天的日期。然后,一次检查一天到未来。如果那天是工作日,请将workingDay计数器递增1.如果不是,则循环到第二天。当您的workingDay计数器达到7时,那就是您需要的日期。
答案 1 :(得分: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);
&#13;
答案 2 :(得分: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());
&#13;