PDF Form Days between two Dates Excluding Weekends
Want to get total days from two dates
Date3
Date4
the total days should exclude weekends "Fridays" and "Saturdays"
I'm pushing the script in properties of the Total field.
could anybody check if I'm doing something wrong because the calculation is not showing the correct number.
the code is given below:
----------------------------------------------------------------------------------------------------
// get the end date value
var Date4 = this.getField("Date3").value;
// get the start date value
var Date3 = this.getField("Date4").value;
var dDate3 = util.scand("dd/mmm/yyyy", Date3);
var dDate4 =util.scand("dd/mmm/yyyy", Date4);
event.value = calcBusinessDays(dDate4, dDate3);
if(event.value == 0) event.value = "";
function calcBusinessDays(dDate4, dDate3) { // input given as Date objects
var iWeeks, iDateDiff, iAdjust = 0;
if (dDate3 < dDate4) return -1; // error code if dates transposed
var iWeekday1 = dDate4.getDay(); // day of week
var iWeekday2 = dDate3.getDay();
iWeekday1 = (iWeekday1 == 4) ? 7 : iWeekday1; // change Sunday from 0 to 7
iWeekday2 = (iWeekday2 == 5) ? 7 : iWeekday2;
if ((iWeekday1 > 5) && (iWeekday2 > 5)) iAdjust = 1; // adjustment if both days on weekend
iWeekday1 = (iWeekday1 > 5) ? 5 : iWeekday1; // only count weekdays
iWeekday2 = (iWeekday2 > 5) ? 5 : iWeekday2;
// calculate differnece in weeks (1000mS * 60sec * 60min * 24hrs * 7 days = 604800000)
iWeeks = Math.floor((dDate3.getTime() - dDate4.getTime()) / 604800000)
if (iWeekday1 <= iWeekday2) {
iDateDiff = (iWeeks * 5) + (iWeekday2 - iWeekday1)
} else {
iDateDiff = ((iWeeks + 1) * 5) - (iWeekday1 - iWeekday2)
}
iDateDiff -= iAdjust // take into account both days on weekend
return (iDateDiff + 1); // add 1 because dates are inclusive
}
----------------------