0

Given a range of dates, number of days needs to be calculated excluding holidays given and certain days each week(weekly offdays).

The holidays will be an array of dates but weekly offdays will just be a number, [0, 1, 2, 3, 4, 5, 6], representing days of the week.

The following is what i have tried so far:

export const calculateWorkingDays = (startDate, endDate, weeklyOffDays, holidays) => {
    // Get difference between start and end dates
    // Not used other parameters yet
    return startDate.diff(endDate, 'days');
}

I dont yet know how to get weekly off days dates from the number representation and also how to subtract given days(weekly off days and holidays) from date range then get remaining days count.

Yunus Einsteinium
  • 1,102
  • 4
  • 21
  • 55

1 Answers1

1

You can loop from startDate to endDate and check for each item if the the day of the week is included in weeklyOffDays or the day is included in the holidays array.

Here a live sample, supposing weeklyOffDays as array of integers and holidays as array of ISO 8601 strings.

const calculateWorkingDays = (startDate, endDate, weeklyOffDays, holidays) => {
  let count = 0;
  const start = moment(startDate);
  const end = moment(endDate);
  while( start.isBefore(end) ){
    if( !weeklyOffDays.includes(start.day()) && !holidays.includes(start.format('YYYY-MM-DD'))){
      count++;
    }
    start.add(1, 'day');
  }
  return count;
}

const res = calculateWorkingDays('2018-07-01', '2018-07-15', [5, 6], ['2018-07-04']);
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
VincenzoC
  • 30,117
  • 12
  • 90
  • 112