0

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);
4
Contributors
4
Replies
30
Views
3 Years
Discussion Span
Last Post by rubberman
0

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);
0

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?

0

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;
    }

Edited by Miiro

2

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

Votes + Comments
I think wars were fought over this.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.