Note that there are some explanatory texts on larger screens.

plurals
  1. POdatepicker national holidays count
    text
    copied!<p>I have 2 inputs where dates are selected #startdate and #enddate. I am using datepicker and have currently disabled weekends and holidays, this is working great!</p> <p>I then have a #days input which calculates the difference between the 2 dates, this date difference then has a weekend count taken from it.</p> <p>I would like to create a holiday count so I can also minus this from the differnce in days.</p> <p>So i would end up with </p> <pre><code>$('#days').val((Math.abs(($d2new-$d1new)/86400000) - weekend_count - holidaycount) + 1); </code></pre> <p><strong>My current code is as follows (this has been taken form other stackoverflow questions and is working great :)</strong> towards the bottom is the var holidaycount which is what i am struggling with. Any help would be greatly appreciated.</p> <pre><code> //holidays var natDays = [ [1, 1, 'uk'], [1, 2, 'uk'], [1, 3, 'uk'], [1, 4, 'uk'], [12, 24, 'uk'], [12, 25, 'uk'], [12, 26, 'uk'], [12, 27, 'uk'], [12, 28, 'uk'], [12, 29, 'uk'], [12, 30, 'uk'], [12, 31, 'uk'] ]; function noWeekendsOrHolidays(date) { var noWeekend = $.datepicker.noWeekends(date); if (noWeekend[0]) { return nationalDays(date); } else { return noWeekend; } } function nationalDays(date) { for (i = 0; i &lt; natDays.length; i++) { if (date.getMonth() == natDays[i][0] - 1 &amp;&amp; date.getDate() == natDays[i][1]) { return [false, natDays[i][2] + '_day']; } } return [true, '']; } // do initialization here $("#startdate").datepicker({ dateFormat: 'dd-mm-yy', changeMonth: true, changeYear: true, firstDay: 1, yearRange: '0:+100', beforeShowDay: noWeekendsOrHolidays, onSelect: function( selectedDate ) { $("#enddate").datepicker("option","minDate",selectedDate ); $("#enddate2").datepicker("option","minDate",selectedDate ); }, minDate: '+1d', maxDate: '+' + DAY_DIFFERENCE + 'd' }); // do initialization here $("#enddate").datepicker({ dateFormat: 'dd-mm-yy', changeMonth: true, changeYear: true, firstDay: 1, yearRange: '0:+100', beforeShowDay: noWeekendsOrHolidays, maxDate: '+' + DAY_DIFFERENCE + 'd' }); // do initialization here $("#enddate2").datepicker({ dateFormat: 'dd-mm-yy', changeMonth: true, changeYear: true, firstDay: 1, yearRange: '0:+100', beforeShowDay: noWeekendsOrHolidays, onSelect: function( selectedDate ) { $d1 = $('#startdate').val(); $d2 = $('#enddate2').val(); $myDateParts1 = $d1.split("-"); $myDateParts2 = $d2.split("-"); $d1flip = new Date($myDateParts1[2], ($myDateParts1[1]-1), $myDateParts1[0]); $d2flip = new Date($myDateParts2[2], ($myDateParts2[1]-1), $myDateParts2[0]); $newdate1 = $d1flip.format("ddd mmm dd hh:MM:ss yyyy"); $newdate2 = $d2flip.format("ddd mmm dd hh:MM:ss yyyy"); // For Opera and older winXP IE n such $d1new = Date.parse($newdate1); $d2new = Date.parse($newdate2); var weekend_count = 0; for (i = $d1new.valueOf(); i &lt;= $d2new.valueOf(); i+= 86400000){ var temp = new Date(i); if (temp.getDay() == 0 || temp.getDay() == 6) { weekend_count++; } } var holidaycount = 0; for (i = $d1new.valueOf(); i &lt;= $d2new.valueOf(); i+= 86400000){ var temp = new Date(i); if (**date in var natDays &amp; is in selected difference range**) { holidaycount++; } } console.log(weekend_count); console.log(holidaycount); $('#days').val((Math.abs(($d2new-$d1new)/86400000) - weekend_count - holidaycount) + 1); } }); }, 'html'); return false; }); } </code></pre> <p><strong>EDIT</strong></p> <p>From the answer, I have inserted the function and tried this.... console.log(holidaycount); returns 0 but with the chosen dates i selected it should have been 10</p> <pre><code> var holidaycount = 0; for (i = $d1new.valueOf(); i &lt;= $d2new.valueOf(); i+= 86400000){ var temp = new Date(i); if (isHoliday(temp)) { holidaycount++; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload