What the script does :
Given a date and time, it adds x number of hours. While doing that it skips off-hours and adds the remaining hour to the next day after opening time. If the next day is a weekend, it skips that as well.
Where I am getting stuck :
Apart from Weekends, I am trying to make it skip an array of dates that are holidays. (The array is on a Google Sheet Column which I have not included in the code) Also, if the number of hours I add makes it go to the next day, the calculation becomes off. For example, if at 17 hours I add 10 hours, the logic breaks.
The code :
function w(d) {
let open_h = 9
let close_h = 18
let work_h = close_h - open_h
let add_h = 11 //number of hours to add. This goes wrong if it goes beyond 00:00:00
d.setDate(d.getDate() + (1 + 7 - d.getDay()) % 7);
let end_date = d
if (end_date.getHours() < open_h) {
end_date.setHours(open_h)
}
end_date.setTime(end_date.getTime() + (add_h*60*60*1000))
if (end_date.getHours()>open_h && end_date.getHours()<close_h) {
} else {
end_date.setTime(end_date.getTime()-(work_h*60*60*1000))
if (end_date.getDay() == 6 || end_date.getDay() == 0 ) {end_date.setDate(next_Monday) } else {
end_date.setDate(end_date.getDate() + 1)
}
}
return end_date
}