Calculate Number of Days between Two Datepickers Excluding Weekends and Disabled Dates

Hello, I need some help using code to calculate the number of days between two datepickers excluding weekends and disabled dates.

User input form is as follows:

  1. User selects a start date (#datePicker1)

  2. User selects an end date (#datePicker2)

  3. User clicks the Calculate Number of Leave Days button (#button1)

  4. Number of Leave Days field should show the number of days between the two dates chosen EXCLUDING weekends and disabled dates (#input3)

Examples are as follows:

  1. Number of Leave Days from 24 April to 27 April should be 2 DAYS → 25 and 26 April are weekends


2. Number of Leave Days from 24 December to 28 December should be 2 DAYS → 25 December is a public holiday (disabled date) and 26 and 27 December are weekends

All weekends and public holidays have been disabled from both datepickers already.

Please advise with the calculation. Thank you.

Hi Dez,

Writing concise and accurate date arithmetic code in Javascript is always a challenge. There are numerous ways to go about it, but here’s an approach that achieves your desired aim:

export function datePicker2_change(event) {
 let d1 = $w("#datePicker1").value;
 let d2 = $w("#datePicker2").value;
 let disDates = $w("#datePicker2").disabledDates;
 let disDOW = $w("#datePicker2").disabledDaysOfWeek;
 let leaveDays = 0, isDisabled = false;
 let incrDate = d1, dow = 0;
 do {
     isDisabled = false;
     dow = incrDate.getDay();
     disDates.forEach((disDate) => {
     // need to loop through array to do this comparison
     if (disDate.toDateString() === incrDate.toDateString()){
         isDisabled = true;
      }
      })  
      if (isDisabled === false && disDOW.indexOf(dow) === -1){
          leaveDays = leaveDays + 1;  
      }
      incrDate.setDate(incrDate.getDate() + 1);
    } while (incrDate <= d2);
    $w("#leaveDays").value = leaveDays;
}

The code works great! Thank you, @tony-brunsman :slight_smile:

how to add this code to wix? please help