1

I'm trying to calculate duration when in the middle of date there is a holiday. I have tried when i choose the holiday date as the end date, the duration was 0. But if i choose next day the duration will calculate as usual without without a reduction in holidays. Whats must I add or change in my code

function getBusinessDatesCount(startDate, endDate) {
  var count = 0;
  var curDate = new Date(startDate);

  var holiday = [];
  holiday[0] = new Date('2019-08-08');
  holiday[1] = new Date('2019-08-14');

  while (curDate <= endDate) {
    var dayOfWeek = curDate.getDay();
    var isWeekend = (dayOfWeek == 6) || (dayOfWeek == 0);

    if (!isWeekend && !isHoliday(endDate, holiday))
      count++;
    curDate.setDate(curDate.getDate() + 1);
  }

  function isHoliday(dt, arr) {
    var bln = false;
    for (var i = 0; i < arr.length; i++) {
      if (compare(dt, arr[i])) { //If days are holidays
        bln = true;
        break;
      }
    }
    return bln;
  }

  function compare(dt1, dt2) {
    var equal = false;
    if (dt1.getDate() == dt2.getDate() && dt1.getMonth() == dt2.getMonth() && dt1.getFullYear() == dt2.getFullYear()) {
      equal = true;
    }
    return equal;
  }
  return count;
}

I expect when the date through the holiday, duration will not calculate the holiday date

adiga
  • 34,372
  • 9
  • 61
  • 83
  • Possible duplicate of [Calculate working days between two dates in Javascript excepts holidays](https://stackoverflow.com/questions/37069186/calculate-working-days-between-two-dates-in-javascript-excepts-holidays) – Jon Koops Aug 06 '19 at 09:13
  • i have tried, but the duration still calculate the holiday –  Aug 06 '19 at 09:51

0 Answers0