Calculate Business Days
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-12-2022 08:11 AM
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;
}
},
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2022 09:12 AM
Thank you for that clarification. I just used those two schedules you've referenced and I changed it to 10 days. Here is the result...
For the example you did, April 1, 10 business days would be 4/15. This worked as expected.
Test number 2, April 4, 10 business days would be 4/18...
This test failed. This is where I keep finding myself. I've found solutions that work initially but given several tests, I always start to get random results. Not sure what that is about.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2022 09:33 AM
Hi
I just figure out one thing it is dependent on timezone as well. Change the timezone to Canada/Eastern
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2022 09:51 AM
Also one more small change,
Change the var days =11 in script include.
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2022 12:57 PM
Gunjan, that is giving much better and consistent results. Now I need to understand why this isn't behaving the same in the US/Pacific timezone.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-13-2022 07:20 PM
Hi Ken,
Try the following schedule and scripts.
Schedule. I've created a new schedule that's "All Day" Mon-Fri to make the calculation easier.
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var numberOfDays = 1;
try {
var startDate = getDateFromFormat(newValue, g_user_date_format); // if user's date format is different from yyyy-MM-dd
startDate = new Date(startDate);
startDate = startDate.getFullYear() + '-' + padZero(startDate.getMonth() + 1) + '-' + padZero(startDate.getDate());
var ajax = new GlideAjax('ScheduleUtil');
ajax.addParam('sysparm_name', 'getBusinessDate');
ajax.addParam('sysparm_start_date', startDate);
ajax.addParam('sysparm_days', numberOfDays);
ajax.getXMLAnswer(function(answer) {
if (answer.length > 0) {
g_form.setValue('end_date', answer);
}
});
} catch (e) {
alert(e.message);
}
function padZero(str) {
return ('00' + str).slice(-2);
}
}
Script Include
var ScheduleUtil = Class.create();
ScheduleUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getBusinessDate: function() {
try {
var startDate = this.getParameter('sysparm_start_date');
var numberOfDays = this.getParameter('sysparm_days');
numberOfDays += .1; // adjustment for calculation error
gdtStartDate = new GlideDateTime();
gdtStartDate.setDisplayValue(startDate);
var dur = new GlideDuration(60 * 60 * 24 * 1000 * numberOfDays);
var sched = new GlideSchedule('b4560c4597d1011086d3b4b3f153afc1');
var gdtEndDate = sched.add(gdtStartDate, dur);
return gdtEndDate.getLocalDate();
} catch (e) {
return e.message;
}
},
type: 'ScheduleUtil'
});
I've tested with several time zone and got the same following results.