Friday 26 June 2009

Add working days to a date

Hi,

when calculating the difference between 2 dates, you might only want to know the working days between those dates and not include the weekends..here is the code below to achieve that.

Pass the date and the no of days to add and it will return you the next date not including weekends.

function addWorkingDays(myDate,days) {
//myDate = starting date, days = no. working days to add.
var temp_date = new Date();
var i = 0;
var days_to_add = 0;
while (i < (days)){
temp_date = new Date(myDate.getTime() +
(days_to_add*24*60*60*1000));
//0 = Sunday, 6 = Saturday, if the date not
equals a weekend day then increase by 1
if ((temp_date.getDay() != 0) &&
(temp_date.getDay() != 6)){
i+=1;
}
days_to_add += 1;
}
return new Date(myDate.getTime() +
days_to_add*24*60*60*1000);
}

Cheers!!

No comments: