0

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
  
}
eceo
  • 179
  • 2
  • 9
  • 1
    And where is that *Array of holidays*? `let add_h = 11` what does 11 represents, the hours from 18 to 9? Clearly that's not 11... sometimes ;) – Roko C. Buljan Aug 30 '21 at 11:57
  • The dates are on a Google Sheet in a column. @RokoC.Buljan – eceo Aug 30 '21 at 12:00
  • Yes, it is close, but the question is related to calculating the number of days between two dates except for holidays. That's not what I am trying to do. I am trying to skip the dates that are holidays and come to the next date. – eceo Aug 30 '21 at 12:05
  • I have added a comment to that line @RokoC.Buljan – eceo Aug 30 '21 at 12:06

0 Answers0