Reputation: 241
I have the following script that when used, allows the user to see a future date, while excluding weekends. The problem i've encountered though is if the current day is Friday, and i set the future date to 3 days it counts the Saturday and Sunday as working days. I'm really hoping one of you may be able to help as I'm not really that great at Javascript.
The correct example would be: If Today = Friday then 3 working days from now would be Wednesday (not Monday as the script currently calculates it).
Any ideas?
var myDelayInDays = 3;
myDate=new Date();
myDate.setDate(myDate.getDate()+myDelayInDays);
if(myDate.getDay() == 0){//Sunday
myDate.setDate(myDate.getDate() + 2);//Tuesday
} else if(myDate.getDay() == 6){//Saturday
myDate.setDate(myDate.getDate() + 2);//Monday
}
document.write('' + myDate.toLocaleDateString('en-GB'));
Any help would really be great. Thanks
Upvotes: 0
Views: 1044
Reputation: 4876
Try this code by changing date and days to add, A custom loop is used to skip sat and sun
function addDates(startDate,noOfDaysToAdd){
var count = 0;
while(count < noOfDaysToAdd){
endDate = new Date(startDate.setDate(startDate.getDate() + 1));
if(endDate.getDay() != 0 && endDate.getDay() != 6){
//Date.getDay() gives weekday starting from 0(Sunday) to 6(Saturday)
count++;
}
}
return startDate;
}
var today = new Date();
var daysToAdd = 3;
alert(addDates(today,daysToAdd));
Upvotes: 1