I\'m trying to set a date to 7 working days from today\'s date (excluding weekends and UK public holidays).
If you only want to add 7 days and exclude holidays and weekends (assuming Saturday and Sunday), then you can just step from the start to the end and test each day as you go:
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());