As stated in the comments, you will need to define an array of holidays.
In this example, I defined two dates: 2016-11-23 and 2016-12-02
script, .
, script MySQL, YYYY-MM-DD.
.
current MySQL, .
while , , , , , " " .
var holiday_array=["2016-11-23", "2016-12-02"];
function dateToMySQL (x){
var MySQL_day = x.getDate();
if(MySQL_day<10){
MySQL_day = "0"+MySQL_day;
}
var MySQL_month = x.getMonth()+1;
if(MySQL_month<10){
MySQL_month = "0"+MySQL_month;
}
var MySQL_year = x.getYear()+1900;
var MySQL_date = MySQL_year+"-"+MySQL_month+"-"+MySQL_day;
return MySQL_date;
}
function calcBusinessDays(start, end) {
var start = new Date(start);
var end = new Date(end);
var totalBusinessDays = 0;
start.setHours(0,0,0,0);
end.setHours(0,0,0,0);
var current = new Date(start);
current.setDate(current.getDate() + 1);
var day;
var holidayFound=false;
while (current <= end) {
var MySQLdate = dateToMySQL(current);
console.log("MySQL date: "+MySQLdate);
if($.inArray(MySQLdate,holiday_array)!=-1){
console.log(" ^----------- Holiday!!!");
holidayFound=true;
}
day = current.getDay();
if (day >= 1 && day <= 5 && !holidayFound) {
++totalBusinessDays;
}
current.setDate(current.getDate() + 1);
holidayFound=false;
}
return totalBusinessDays;
}
$(function() {
$( "#start_date" ).datepicker({
minDate:0,
showOn: 'button',
buttonImageOnly: true,
buttonImage: 'http://www.nscale.net/forums/images/misc/Tab-Calendar.png',
beforeShowDay: $.datepicker.noWeekends
});
$( "#end_date" ).datepicker({
minDate:0,
showOn: 'button',
buttonImageOnly: true,
buttonImage: 'http://www.nscale.net/forums/images/misc/Tab-Calendar.png',
beforeShowDay: $.datepicker.noWeekends,
onSelect: function (dateStr) {
var max = $(this).datepicker('getDate');
$('#datepicker').datepicker('option', 'maxDate', max || '+1Y+12M');
var start = $("#start_date").datepicker("getDate");
var end = $("#end_date").datepicker("getDate");
var days = (end - start) / (1000 * 60 * 60 * 24);
var diff = calcBusinessDays(start,end);
$("#leave_days").val(diff);
}
});
});
CodePen ( ;))