Reputation: 708
I have a function which calculates a date that is passed and adds a given number of days to the result ensuring the outputted date doesn't fall on a weekend.
The function
function addWorkingDays(datStartDate, lngNumberOfWorkingDays, blnIncSat, blnIncSun) {
var intWorkingDays = 5;
var intNonWorkingDays = 2;
var intStartDay = datStartDate.getDay(); // 0=Sunday ... 6=Saturday
var intOffset;
var intModifier = 0;
if (blnIncSat) { intWorkingDays++; intNonWorkingDays--; }
if (blnIncSun) { intWorkingDays++; intNonWorkingDays--; }
var newDate = new Date(datStartDate)
if (lngNumberOfWorkingDays >= 0) {
// Moving Forward
if (!blnIncSat && blnIncSun) {
intOffset = intStartDay;
} else {
intOffset = intStartDay - 1;
}
// Special start Saturday rule for 5 day week
if (intStartDay == 6 && !blnIncSat && !blnIncSun) {
intOffset -= 6;
intModifier = 1;
}
} else {
// Moving Backward
if (blnIncSat && !blnIncSun) {
intOffset = intStartDay - 6;
} else {
intOffset = intStartDay - 5;
}
// Special start Sunday rule for 5 day week
if (intStartDay === 0 && !blnIncSat && !blnIncSun) {
intOffset++;
intModifier = 1;
}
}
// ~~ is used to achieve integer division for both positive and negative numbers
newDate.setDate(datStartDate.getDate() + new Number((~~((lngNumberOfWorkingDays + intOffset) / intWorkingDays) * intNonWorkingDays) + lngNumberOfWorkingDays + intModifier));
return newDate;
}
I need to now add some dates to this function for it to take into account, these are public holiday's in Sydney Australia. What I am looking to achieve is the following.
var newDate = addWorkingDays('30/09/2015', 3, false, false);
In this example the returned date would fall on the 03/10/2015
which is a public holiday and therefore we would need to return the 04/10/2015
The dates in question are:
01/01/2015 New Year's Day
26/01/2015 Australia Day
09/03/2015 Labour Day / Eight Hours Day / Adelaide Cup / Canberra Day
03/04/2015 Good Friday
06/04/2015 Easter Monday
25/04/2015 ANZAC Day
08/06/2015 Queen's Birthday
03/08/2015 Bank Holiday / Picnic Day
05/10/2015 Labour Day
03/11/2015 Melbourne Cup
25/12/2015 Christmas Day
26/12/2015 Boxing Day
Given the function above can someone help me to amend the function to take these dates into account.
Upvotes: 1
Views: 89
Reputation: 580
As it is your code gives me errors when I try to call it like you specify, but regardless you should simply be able to check if the date you're going to give as an answer is one of your invalid dates, and if so then return the next working day:
(pseudocode)
newDate.setDate(datStartDate.getDate() + new Number((~~((lngNumberOfWorkingDays + intOffset) / intWorkingDays) * intNonWorkingDays) + lngNumberOfWorkingDays + intModifier));
if (newDate == "01/01/2015" || newDate == "26/01/2015" || ....) {
return addWorkingDays(newDate, 1, blnIncSat, blnIncSun);
}
else {
return newDate;
}
Upvotes: 1