user1033038
user1033038

Reputation: 141

Calculate future date excluding holidays and weekends

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 not working.

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

Upvotes: 3

Views: 3376

Answers (5)

Avinash Singh
Avinash Singh

Reputation: 5392

Just have the same requirement and this is the my work around.Hope it helps other

var holiday = ["4/18/2019", "4/19/2019", "4/20/2019", "4/25/2019", "4/26/2019"];
var startDate = new Date();
var endDate = new Date(startDate.setDate(startDate.getDate() + 1));

for (i = 0; i < holiday.length; i++) {
    var date = endDate.getDate();
    var month = endDate.getMonth() + 1; //Months are zero based
    var year = endDate.getFullYear();
    if ((month + '/' + date + '/' + year) === (holiday[i])) {
        endDate = new Date(endDate.setDate(endDate.getDate() + 1));
        if (endDate.getDay() == 6) {
            endDate = new Date(endDate.setDate(endDate.getDate() + 2));
        } else if (endDate.getDay() == 0) {
            endDate = new Date(endDate.setDate(endDate.getDate() + 1));
        }
    }
}

Here, end date gives you next working day.Here,I'm ignoring current day and start comparing from Next day whether it's holiday or weekend.You can customize dateTime as per your requirement (month + '/' + date + '/' + year).Careful whenever you compares two dates with each other. Because it looks same but actually it's not.So customize accordingly.Its calculate future date excluding holidays and weekends

Upvotes: 2

Jarek
Jarek

Reputation: 1

Some mistakes but good idea. This is correct:

var holiday = [];
    holiday[0] = new Date(2018, 10, 01);//remember that month is 0 to 11
    holiday[1] = new Date(2018, 10, 11);//remember that month is 0 to 11
	holiday[2] = new Date(2018, 11, 25);//remember that month is 0 to 11
	holiday[3] = new Date(2018, 11, 26);//remember that month is 0 to 11
	holiday[4] = new Date(2019, 00, 01);//remember that month is 0 to 11
	
	var a = Date.parse(document.getElementById(1).value);
	var b = Date.parse(document.getElementById(2).value);
	
    var startDate = new Date(a);
    var endDate = new Date(b);
	//var noOfDaysToAdd = 8;
	var count = 0;
	var czydata = false;
    
	if (startDate > endDate)
	{
		alert("POPRAW DANE!!! Data OD nie moze byc wieksz od Daty DO");
	}
	else
	{
		while (czydata == false) {
			czydata = cmpday(startDate,endDate)
			 // Date.getDay() gives weekday starting from 0(Sunday) to
			// 6(Saturday)
			if (startDate.getDay() != 0 && startDate.getDay() != 6 && !isHoliday(startDate,holiday)) {
				
				count++;
				
			}
			startDate.setDate(startDate.getDate()+1);
		}
	}
	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;
	
	}
	
	function cmpday(date1, date2){
	var eqdate = false;
	if(date1.getDate() == date2.getDate() && date1.getMonth() == date2.getMonth() && date1.getFullYear() == date2.getFullYear()) {
		eqdate = true;
	}
	return eqdate;
	
	}

Upvotes: -1

Chitra
Chitra

Reputation: 1

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

Upvotes: 0

Ankit
Ankit

Reputation: 3183

Run the loop and count the no. of holidays between the start and end date. Add this count to the noOfDaysToAdd and then add this value to start date to get the final date.

Edit:

You need to correct two thing from your logic:

1) The date comparison is not correct, when you create a holiday date, you are just passing the date, month and year. and when you do a new Date(), you get a date with time as well. Try to alert both these dates and see the difference. Due to this difference, the date comparison will always comes out to be unequal.

2) The other problem is that you are adding count value in you for loop. So for each date, the count value is getting incremented by the no. of holidays which you have in your holiday array. You need to correct this too.

Upvotes: 0

Lauri Elias
Lauri Elias

Reputation: 1299

In your case, the for loop runs twice every time and every time it runs and the next day is not in your predefined array, it adds 1 to the count, thereby finishing the routine after just 7 working days and giving you the wrong date. Add more holidays and you'll miss even more.

Upvotes: 0

Related Questions