I am using below code which will count total days if cb1
is checked.
If cb1
is unchecked (default state) weekends will be excluded.
My 1st problem is that it is showing one day less in calculation once cb1
is checked and 2nd problem is that when cb1
is unchecked it is not showing any result.
Any help, please. I took reference from this thread.
(function () {
if (getField("cb1").value != "Off") {
var Start = this.getField("LeaveFrom").value;
var End = this.getField("LeaveEnd").value;
var dStart = util.scand("dd/mm/yyyy H:MM:SS", Start + " 0:00:00");
var dEnd = util.scand("dd/mm/yyyy H:MM:SS", End + " 0:00:00");
var diff = dEnd.getTime() - dStart.getTime();
var oneDay = 24 * 60 * 60 * 1000;
var days = Math.floor(diff/oneDay);
event.value = days;
} else {
var start = this.getField("LeaveFrom").value; // get the start date value
var end = this.getField("LeaveEnd").value; // get the end date value
var start =util.scand("dd/mm/yyyy H:MM:SS", start + " 0:00:00");
var end = util.scand("dd/mm/yyyy H:MM:SS", end + " 0:00:00");
event.value = dateDifference(start, end);
function dateDifference(start, end) {
if (end < start) return -1;
// Copy date objects so don't modify originals
var s = new Date(+start);
var e = new Date(+end);
// Get the difference in whole days
var totalDays = Math.round((e - s) / 8.64e7);
// Get the difference in whole weeks
var wholeWeeks = totalDays / 7 | 0;
// Estimate business days as number of whole weeks * 5
var days = wholeWeeks * 5;
// If not even number of weeks, calc remaining weekend days
if (totalDays % 7) {
s.setDate(s.getDate() + wholeWeeks * 7);
while (s < e) {
s.setDate(s.getDate() + 1);
// If day isn't a Friday or Saturday, add to business days
if (s.getDay() != 5 && s.getDay() != 6) {
++days;
}
}
}
return days;
}
}
})();