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

Hi Ken

I have couple of doubts :

1) What is the field type of both date fields?is it date Time or date field only ?

2) The dates on that fields are comming from somewhere else or you are entering dates into that fields manually?

 

Please answer above questions so that I can write script accordingly.

 

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

1) The field type is Date for both fields. Date only. The result field is returning a timestamp because I haven't modified the copy/paste script i used in a previous response to this question. 

 

2) The dates are being manually selected in the Start field. The script in the background runs some calculations and the End date is populated automatically by the result of the script. This is done via onChange Client Script.

Hi @Ken Pruitt,

 

I'm assuming all these fields are on the catalog if yes then write onchange catalog client script as below on the start date:-  

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type appropriate comment here, and begin script below
//Show end date after 10 days
var ga = new GlideAjax("CheckSchedule");
ga.addParam("sysparm_name", "isInSchedule");
ga.addParam("sysparm_date", newValue);
ga.getXML(parseResponse);

function parseResponse(response) {
var answer =response.responseXML.documentElement.getAttribute("answer");
//alert(answer);
if (answer != '') {
g_form.setValue('end_date',answer);
}
}

}   

 

find_real_file.png

 

Script Include:-  

 

var CheckSchedule = Class.create();
CheckSchedule.prototype = Object.extendsObject(AbstractAjaxProcessor, {
isInSchedule: function() {
var selected_date = this.getParameter("sysparm_date");
gs.log("Date is " + selected_date);
var d = new GlideDateTime();
d.setDisplayValue(selected_date);
//mention your schedule sys_id here.
var schedule = new GlideSchedule('090eecae0a0a0b260077e1dfa71da828');
var gdt = new GlideDateTime();
schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //check your schedule and take a scheudle sysid which takes only week days and exclude weeknds.
var days = 9;
var dur = new GlideDuration(60*60*9*1000*days);
gs.info('Duration is'+dur);
var end = schedule.add(d, dur);
gs.info('end'+end);
var value = end.getDate();
gs.info('value'+value);

return value;

},
type: 'CheckSchedule'
});   

 

 

find_real_file.png

 

Output:-

find_real_file.png

14th is the 10th Business Day as pe calender :-

find_real_file.png

 

Please mark my answer as helpful/correct if it resolves your query.

 

Regards,

Gunjan Kiratkar

Consultant - ServiceNow, Cloudaction

Rising Star 2022

 


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

@Gunjan Kiratkar Thank you for the thorough response. I copy/pasted what you listed above. Here is my result...

find_real_file.png

Similar to what I am getting already. It counted 9 days from the selected date.

- It should be 10 days. I assume this was due to passing in 9 to the script include duration calculation on Line 13

- The result should not be a weekend date. Could this be some sort of configuration issue with my schedule? I'm not sure what the two schedule sys_ids were for in the script as one of them was just being changed and ultimately ignored.

Hi @Ken Pruitt ,

 

First Sys_ID of scheduled and Configuration:-

find_real_file.png

find_real_file.png

 

2nd Sys_ID :-

find_real_file.png

find_real_file.png

 

Please mark my answer as helpful/correct if it resolves your query.

 

Regards,

Gunjan Kiratkar

Consultant - ServiceNow, Cloudaction

Rising Star 2022

 


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