Flatpickr and jQuery to find business days between two dates - It works! | XM Community
Skip to main content

Was originally posted this to ask for help, but I have worked out the kinks and this works if anyone wants to use it. I assembled from other codes/advice I found (and owe much learning to Tom G). This is taking two dates from datepickr calendar and giving back the correct number of working days in between. The jQuery can be edited if specific holidays need to be edited.
I have this in the header: ">https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.3/flatpickr.js">
https://cdnjs.cloudflare.com/ajax/libs/flatpickr/4.6.3/flatpickr.css" rel="stylesheet" />">https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.min.js">
The html for the question is this (It makes the two text entry fields to display the datepickr and "calc" button to get answer):



Biweekly Pay Periods:



This is my javascript (posted below)



var d1 = flatpickr('#d1', {});
var d2 = flatpickr('#d2', {});

jQuery(document).ready(() => {
  jQuery('#calc').click(() => {
 var d1 = $('#d1').val();
  var d2 = $('#d2').val();
  jQuery('#dif').text(workingDaysBetweenDates(d1,d2));
  });
});

let workingDaysBetweenDates = (d0, d1) => {
  /* Two working days and an sunday (not working day) */
  var holidays = d'2016-05-03', '2016-05-05', '2016-05-07'];
  var startDate = parseDate(d0);
  var endDate = parseDate(d1);

console.log(startDate);
console.log(endDate);
Qualtrics.SurveyEngine.setEmbeddedData ( "startDate", startDate );
Qualtrics.SurveyEngine.setEmbeddedData ( "endDate", endDate );

// Validate input
  if (endDate < startDate) {
  return 0;
  }

// Calculate days between dates
  var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
  startDate.setHours(0, 0, 0, 1); // Start just after midnight
  endDate.setHours(23, 59, 59, 999); // End just before midnight
  var diff = endDate - startDate; // Milliseconds between datetime objects
  var days = Math.ceil(diff / millisecondsPerDay);


  // Subtract two weekend days for every week in between
  var weeks = Math.floor(days / 7);
  days -= weeks * 2;

  // Handle special cases
  var startDay = startDate.getDay();
  var endDay = endDate.getDay();
 
  // Remove weekend not previously removed.
  if (startDay - endDay > 1) {
  days -= 2;
  }
  // Remove start day if span starts on Sunday but ends before Saturday
  if (startDay == 0 && endDay != 6) {
  days--;
  }
  // Remove end day if span ends on Saturday but starts after Sunday
  if (endDay == 6 && startDay != 0) {
  days--;
  }
  /* Here is the code */
  holidays.forEach(day => {
  if ((day >= d0) && (day <= d1)) {
  /* If it is not saturday (6) or sunday (0), substract it */
  if ((parseDate(day).getDay() % 6) != 0) {
  days--;

  }
} Qualtrics.SurveyEngine.setEmbeddedData ( "DaysBetween", days);
  });
  return days ;
}

function parseDate(input) {
  // Transform date from text to date
  var parts = input.match(/(\\d+)/g);
  // new Date(year, month m, date \, hours;, minutes , secondsy, ms]]]]])
  return new Date(parts[0], parts[1]-1, partsb2]); // months are 0-based
}



Leave a Reply