This is a tricky situation.
I am trying to subtract Holidays that I manually put into the shown Javascript in order to calculate the days a person is trying to request off. I was able to get the script to remove weekends as well as half days from the form, but holidays are a whole other issue.
Attached is the script, along with an image of the form.
$(document).ready(function(){
var holidays = ["2024-05-28", "2024-11-28", "2024-12-24", "2024-12-25", "2024-12-31"];
function isCorrectDate(n) {
var date = jQuery.datepicker.formatDate('yy-mm-dd', n);
if (holidays.indexOf(date) != -1) {
return false;
} else {
var t = n.getDay();
return (t!=6 && t != 0);
}
}
function checkDate(n) {
return[isCorrectDate(n),""];
}
function checkField(input, inst) {
if ($(input).closest('li').hasClass('checkDate')) {
$(input).datepicker("option", {beforeShowDay: checkDate});
}
}
$.datepicker.setDefaults( {beforeShow: checkField} );
window.Parsley.addValidator('noweekend', {
validateString: function(value) {
console.log(value);
return isCorrectDate(new Date(value));
},
messages: {
en: 'Not valid date.'
}
});
$('.checkDate input').attr('data-parsley-noweekend','');
})
function sumtotal() {
var s = 0;
var e = 0;
//Get dates from input and reformat, then create date var
$('.sdate input').each(function () {
s1 = $(this).val().replace("-", " ");
s2 = s1.replace("-", " 20");
s = new Date(s2);
});
$('.edate input').each(function () {
e1 = $(this).val().replace("-", " ");
e2 = e1.replace("-", " 20");
e = new Date(e2);
});
var one_day = 1000 * 60 * 60 * 24;
var startMillis = s.getTime();
var endMillis = e.getTime();
var totalDays = Math.round((endMillis - startMillis)/one_day);
var weekend = 0;
var days = 0;
var holidays = 0;
for (var i = startMillis; i < endMillis; i += one_day) {
var currentDay = new Date(i);
if (currentDay.getDay() == 5 || currentDay.getDay() == 6) {
weekend++;
}
}
//Check whether the half day radio button is "Yes"
var choice = $('.halfday :checked').val();
if (choice == 'Yes') {
days = (totalDays+1) - 0.5 - weekend - holidays;
} else {
days = (totalDays+1) - weekend - holidays;
}
//Output value in the total class box
if (isNaN(days)) {
$('.total input').val('Select dates');
} else {
$('.total input').val(days);
}
if (e.getTime() < s.getTime()) {
$('.edate input').val($('.sdate input').val());
sumtotal();
}
}
$(document).ready(function () {
$(".sdate input").on('blur change', sumtotal);
$(".edate input").on('blur change', sumtotal);
$(".halfday input").on('blur change', sumtotal);
$(".read-only *").prop("readonly", true);
});