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

That Schedule is the same as everyone has, the OOTB 8-5 weekdays schedule.

@Shane J,

If screenshot is shared it would be helpful.

Also as mentioned in previous comment, update the durations in seconds as per the Schedule timings.

 

Thanks