Adding days to a Date is covered in Add days to JavaScript Date.
To avoid weekends (assuming Saturday and Sunday, some places use different days like Thursday and Friday) you can check the result and move forward if the day is 6 (Saturday) or 0 (Sunday). Similarly you can check for holidays using formatted date strings (easier than using Date objects).
A simple algorithm is to add the required days to a date, then if it lands on a weekend or holiday, keep adding days one at a time until it doesn't.
// Utility functions
// Add days to a date
function addDays(date, days) {
date.setDate(date.getDate() + days);
return date;
}
// Return true if date is Sat or Sun
function isWeekend(date) {
return date.getDay() == 0 || date.getDay() == 6;
}
// Return true if date is in holidays
function isHoliday(date) {
return holidays.indexOf(formatISOLocal(date)) != -1;
}
// Show formatted date
function showDate(date) {
return date.toLocaleString(undefined, {
weekday:'long',
day: 'numeric',
month: 'long',
year: 'numeric'
});
}
// Return date string in YYYY-MM-DD format
function formatISOLocal(d) {
function z(n){return (n<10? '0':'')+n}
return d.getFullYear() + '-' + z(d.getMonth()+1) + '-' + z(d.getDate());
}
// Main function
// Special add days to avoid both weekends and holidays
function specialAddDays(date, days) {
// Add days
date.setDate(date.getDate() + days);
// While date is a holiday or weekened, keep adding days
while (isWeekend(date) || isHoliday(date)) {
addDays(date, 1);
}
return date;
}
// Holidays Tuesday Wednesday Eid al-Adha Christmas
var holidays = ['2017-08-22','2017-08-23', '2017-09-01', '2017-12-25'];
// Examples
var d = new Date(2017,7,18,1,30);
console.log('Start day: ' + showDate(d)); // Friday
specialAddDays(d, 1);
console.log('Day added: ' + showDate(d)); // Monday
// Tue and Wed are (fake) holidays, so should return Thursday
specialAddDays(d, 1);
console.log('Day added: ' + showDate(d));