How do I calculate the end date and time?

I know the amount of time spent on work, I can output the date and time of the end of work, but without taking into account working hours...and weekends. For example, the work will be done for 5 hours, the working hours are from 8 to 16, the work started at 14 hours, respectively, how do I calculate the time to show that it will end at 11 hours + 1 day

Tried through get Day, etc. You can't do with just checking if else because you need to check both the input data and the output data Maybe who knows how to make a clock that has 8 hours in a day?

t = new Date()
 firstday = true
 var rest = 5   //рест колво часов на выполнение задания
 
  while (rest > 0){
  if(t.getDay() == 0||6){  //проверяем на выходные
   t.setDate(t.getDate()+ 1)} //пропускаем если выходной
   else {
   if(firstday){
    starthour = t.getHours() //если начинаем с середины дня 
    firstday = false        
    }
    else
    starthour = 10     //начинаем с начала дня
   ti = (19 - starthour) 
   if (rest > ti){ 
     rest = rest - ti
      t.setDate(t.getDate()+ 1)
  }
  else 
   return (t.getDate(), starthour + rest) 
  };
  }

1 answers

Taking into account the holidays and the postponement of the weekend, it is most reliable to iterate day by day

 пока остаток часов > 0:
     if праздник, выходной: 
         проматываем день
     else (рабочий день): 
        if первый день:
              начало дня = старт работы
        else: 
              начало дня = утро
        t = (вечер - начало дня)
        if осталось больше t:
            отнимаем t часов, проматываем день
        else:
            возвращаем текуший день, начало дня + остаток часов

I'll try to modify what you got:

t = new Date()
firstday = True
let rest = 5
while (rest > 0){
  if(t.getDay() == 0||6){  
    t.setDate(t.getDate()+ 1)
  }
  else {
    if(firstday){
        starthour = t.getHours()
        firstday = False
    }
    else
        starthour = 10
  ti = (19 - starthour) 
  if (rest > ti){ 
         rest = rest - ti
          t.setDate(t.getDate()+ 1)
  }
  else 
    return t.getDate(), starthour + rest 
}

Working code in Python.

def calcworkend(rest, startday, starthour, morning, evening):
    day = startday
    while rest > 0:
        if (day + 1) % 7 < 2:
            day += 1
        else:
            workstart = starthour if day == startday else morning
            workstart = min(evening, max(workstart, morning))
            hoursleft = evening - workstart
            if rest > hoursleft:
                rest -= hoursleft
                day += 1
            else:
                return (day - startday, workstart + rest)

print(calcworkend(8, 1, 15, 10, 19)) 
(1, 14)    # 4 часа в понедельник, 4 во вторник
print(calcworkend(8, 1, 3, 10, 19))
(0, 18)    # рано озадачили, в понедельник управились
print(calcworkend(8, 1, 22, 10, 19))
(1, 18)    # поздно озадачили, во вторник все делали
print(calcworkend(8, 5, 15, 10, 19))
(3, 14)  # в пятницу начали, в понедельник закончили
print(calcworkend(8, 7, 15, 10, 19))
(1, 18)  # в воскресенье озадачили, в понедельник поработали
print(calcworkend(32, 4, 18, 10, 19))
(6, 14)  # в четверг начали большое дело, в среду закончили
 0
Author: MBo, 2020-08-18 17:15:41