Nishu Tayal Nishu Tayal - 5 months ago 18
jQuery Question

Add no. of days in a date to get next date(excluding weekends)

I have an date, i need to add no. of days to get future date but weekends should be excluded.
i.e

input date = "9-DEC-2011";
No. of days to add = '13';

next date should be "27-Dec-2011"


Here weekends(sat/sun) are not counted.

Answer

Try this

var startDate = "9-DEC-2011";
startDate = new Date(startDate.replace(/-/g, "/"));
var endDate = "", noOfDaysToAdd = 13, 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++;
    }
}
alert(endDate);//You can format this date as per your requirement

Working Demo

Comments