As per question, I would like to find the future date based on a given number of days. It should exclude weekends and holidays that is stored as array. Have this code below but the future date generated is incorrect. Any suggestions?

``````   var holiday = [];
holiday[0] = new Date(2013, 11, 12);
holiday[1] = new Date(2013, 11, 13);

var startDate = new Date();
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)
for ( var i = 0; i < holiday.length; i++) {
if (endDate != holiday[i]) { //If days are not holidays
count++;
}
}
}
}
alert(endDate);
``````

## All 4 Replies

This should work. Gives me November 27 as the result when run on November 6.

``````var holiday = [];
holiday[0] = new Date(2013, 10, 12);//remember that month is 0 to 11
holiday[1] = new Date(2013, 10, 13);//remember that month is 0 to 11
var startDate = new Date();
var endDate = new Date(), noOfDaysToAdd = 13, count = 0;

while (count < noOfDaysToAdd) {

endDate.setDate(endDate.getDate()+1)

// Date.getDay() gives weekday starting from 0(Sunday) to
// 6(Saturday)
if (endDate.getDay() != 0 && endDate.getDay() != 6 && !isHoliday(endDate, holiday)) {
count++;
}
}

function isHoliday(dt, arr){
var bln = false;

for ( var i = 0; i < arr.length; i++) {
if (compare(dt, arr[i])) { //If days are not holidays
bln = true;
break;
}
}

return bln;
}

function compare(dt1, dt2){
var equal = false;

if(dt1.getDate() == dt2.getDate() && dt1.getMonth() == dt2.getMonth() && dt1.getFullYear() == dt2.getFullYear()) {
equal = true;
}

return equal;
}

alert(endDate);
``````

does this code compute dates between months eg. 1/aug/2017 to 30/sep/2017 where by it can give you an end date, and my other question is the var holiday [] are u adding the holidays manually?

i modified your code and the extract function better works this way.

`````` function returnfinaldate() {

var holiday = [];
holiday[0] = new Date(2017, 9, 5);// holiday 1
holiday[1] = new Date(2017, 9, 6);//holiday 2

var startDate = new Date();
var endDate = new Date();

startDate = new Date(2017, 7, 1);// '8/1/2017';

noOfDaysToAdd = 7, count = 0;
endDate = startDate;
//use below code to include start date in count else comment below code
//  if (endDate.getDay() != 0 && endDate.getDay() != 6 && !isHoliday(endDate, holiday)) {
//   count++;
// }
while (count < noOfDaysToAdd) {
endDate.setDate(endDate.getDate() + 1)
// Date.getDay() gives weekday starting from 0(Sunday) to
// 6(Saturday)
if (endDate.getDay() != 0 && endDate.getDay() != 6 && !isHoliday(endDate, holiday)) {
count++;
}
}

return endDate;
}``````

There is no way but the right way to compute dates - Julian and Gregorian. :-)

commented: I think wars were fought over this. +12
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.