I am using jquery and moment js to calculate Business Days. For some strange reasons 31 December 2022 is a Saturday but my code is still showing it as working day. I have a start Date of 30th December, 2022 and 1 (one) workDaysToAdd which falls on Saturday the 31st, December 2022 which is not working Business day. My code is not skipping the saturday 31st, December 2022.
function AddBusinessDays(startDate, workDaysToAdd) {
var curDate = new Date(startDate);
var realDaysToAdd = 0;
while (workDaysToAdd > 0) {
realDaysToAdd++;
if (IsWorkDay(curDate)) {
workDaysToAdd--;
}
curDate.setDate(curDate.getDate() + 1);
}
return realDaysToAdd;
}
function IsWorkDay(date) {
var curDate = new Date(date);
var dayOfWeek = curDate.getDay();
var isWorkDay;
if (dayOfWeek == 0 || dayOfWeek == 6) {
isWorkDay = false;
}
else {
isWorkDay = !isNationalDay(curDate);
}
return isWorkDay;
}
function isNationalDay(date) {
var thisDate = new Date(date);
var thisMoment = moment(thisDate);
var holiday = thisMoment.holiday();
var isHoliday = holiday != 'undefined' && holiday != null && holiday.length > 0;
return isHoliday;
}