Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

demonz's avatar

urgent my calculate end date function doesn't skip holidays

hi , i want to make the case for example if a lesson is every Thursday and we had a holiday on one of the month Thursday it should skip the date to the next Thursday but it doesn't , help please.

const holidays = [
    new Date(2023, 0, 1),
    new Date(2023, 2, 9),
    new Date(2023, 3, 21),
    new Date(2023, 3, 22),
];

function calculateEndDate(startDate, courseDuration, workingDays, workingHours) {
    let currentDate = startDate;
    let remainingDuration = courseDuration;

    while (remainingDuration > 0) {
      if(holidays.includes(currentDate)) {
        currentDate.setDate(currentDate.getDate() + 1);
        continue;
      }

      let hoursOnCurrentDay = 0;

      if (workingDays.includes(currentDate.getDay())) {
        hoursOnCurrentDay = workingHours[currentDate.getDay()];
      }

      remainingDuration -= hoursOnCurrentDay;
      currentDate.setDate(currentDate.getDate() + 1);
    }

    return currentDate;
  }



  let startDate = new Date(2023, 0, 19);
  let courseDuration = 36;
  let workingDays = [4]; // Thursday
  let workingHours = {
    4: 4, // 4 hours every Thursday

  };

  let endDate = calculateEndDate(startDate, courseDuration, workingDays, workingHours);
  console.log(endDate);
0 likes
2 replies
demonz's avatar
demonz
OP
Best Answer
Level 2

I solved it by changing .includes and compare it by date.getTime()

Please or to participate in this conversation.