Calculate Business Days

Ken Pruitt
Tera Contributor

Hello Community,

I need to calculate 10 business days in the future from any given date. I'm using the script below but it is not counting business days, it is just counting 10 days out which is incorrect. How do I calculate the date 10 business days out?

getbusinessdays : function getbusinessdays(date, numberOfDays) {
		var schedule = "968d3fbe1b35b3408ed610ad2d4bcbd";
		
		var glideSchedule = new GlideSchedule();
		glideSchedule.load(schedule);
		
		var start = new GlideDateTime();
		start.setDisplayValue(date);
		
		var actualNumberOfDays = 0;
		
		if(glideSchedule.isInSchedule(start)){
			for(var x = 0; x < numberOfDays; x++){
				start.addDays(1);
				
				if(glideSchedule.isInSchedule(start)){
					actualNumberOfDays++;
				}
				else{
					if(start.getDayOfWeek() == 6){
						start.addDays(2);
						actualNumberOfDays++;
					}
					else if(start.getDayOfWeek() == 7){
						start.addDays(1);
						actualNumberOfDays++;
					}
				}
			}
			return start;
		}
		else{
			if(start.getDayOfWeek() == 6){
				start.addDays(2);
				actualNumberOfDays++;
			}
			else if(start.getDayOfWeek() == 7){
				start.addDays(1);
				actualNumberOfDays++;
			}
			if(glideSchedule.isInSchedule(start)){
				for(var y = 0; y < numberOfDays; y++){
					start.addDays(1);

					if(glideSchedule.isInSchedule(start)){
						actualNumberOfDays++;
					}
					else{
						if(start.getDayOfWeek() == 6){
							start.addDays(2);
							actualNumberOfDays++;
						}
						else if(start.getDayOfWeek() == 7){
							start.addDays(1);
							actualNumberOfDays++;
						}
					}
				}
			}
			return start;
		}
    },
31 REPLIES 31

Gunjan Kiratkar
Kilo Patron
Kilo Patron

Hi,

You can check the below thread to calculate business days using a schedule.

How to calculate 5 working days using scehdule and display the future date

 

Regards,

Gunjan


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Thank you, i'm trying this out now...

As a copy/paste, it's calculating 10 days out from today. The use case I have will not always start on today.

find_real_file.png

This is the issue I have run into using various scripts to accomplish this. In this example, the 10th day falls on the weekend(non-business day). I tried accounting for this using the isInSchedule() method but that lead to similar strange results.

find_real_file.png