'Check date range with an array

I have an array, in which I have inserted some day that are holidays.

In my app I choose an interval (startDate and endDate) and I should check that this interval is not in my array. How can I do?

For example:

holidaysArray = [
"2020-04-12",
            "2020-04-13",
            "*-03-17",
            "*-05-01",
            "*-06-02"
}

I choose as

startDate:  2022-03-15T16:16:00.000Z 
endDate:2022-03-18T16:12:23.103Z

What I expect

So now I should count the day between the startDate and EndDate, and the result is 4 days. But I my array there is value "*-03-17", so I should not considering a day.

How can I do?

I have a datepicker:

<DatePicker
            modal
            open={showStartDate}
            date={startDate}
            title={null}
            confirmText="Ok"
            cancelText="Cancel"
            mode="date"
            onConfirm={(date) => {
              setShowStartDate(false)
              onChangeStart( date)
            }}
            onCancel={() => setShowStartDate(false)}
          />

<DatePicker
            modal
            open={showEndDate}
            date={endDate}
            title={null}
            confirmText="Ok"
            cancelText="Cancel"
            mode="date"
            onConfirm={(date) => {
              setShowEndDate(false)
              onChangeStart( date)
            }}
            onCancel={() => setShowEndDate(false)}
          />

So my

onChangeStart = (selectedDate) => {
   checkDate(selectedDate, endDate)
}

checkDate = (start, end) => {
  const holidayArray = travelInfo.time.holidayArray
  //
}


Solution 1:[1]

When you iterate over 'holidayArray' you need to check if the holiday falls between the selected date, if it does just decrement the total difference between the dates by '1' (assuming you are counting Sundays' as well). For the case where the dates have *, you need to find the 'years' that can fall between the dates and use them to replace the * to form the appropriate date.

First Step (Find year (or years if the selection is too large, just in case))

(assuming endDate > startDate)

const findYears = (startDate, endDate) => {
    let yearsArray = [];
    let endYear = parseInt(endDate?.split("-")[0]);
    let startYear= parseInt(startDate?.split("-")[0]);
    yearsArray.push(endYear);
    while(endYear !== startYear){
         endYear--;
         yearsArray.push(endYear);
    }
  return yearsArray;
}

Now complete your checkDate function

checkDate = (start, end) => {
    const years = findYears(start, end)
    const holidayArray = travelInfo.time.holidayArray;
    var from = new Date(start);
    var to = new Date(end);
    var diff = 0;
    holidayArray.forEach(holiday => {
        if (holiday.match("\\*")) {
            years.forEach(year => {
                var check = new Date(holiday.replace("\\*", year));
                if (check > from && check < to) {
                    diff--
                }
            })
        } else {
            var check = new Date(holiday);
            if (check > from && check < to) {
                diff--
            }
        }
    })
    const diffTime = Math.abs(start - end);
    var diffDays = Math.round(diffTime / (1000 * 60 * 60 * 24));
    return diffDays + diff
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Pranay Nailwal