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
12-19-2022 07:26 AM
Shows something like this is worth testing - seems like if the original 'end day' based on the number of days is a Saturday (if you ran it for a Monday, for 12, 19, 26 etc days) , you don't get the expected result.
Trying to account for that.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 01:52 PM - edited 12-20-2022 06:39 AM
@Ken Pruitt I think I got it, with a much simpler script. Should be able to run this as a Background Script or Fix Script.
BusDaysScratchOrigv3();
function BusDaysScratchOrigv3() {
var days = 15;
gs.print('No of days ' + days);
var startDate = new GlideDateTime();
if (days > 4) {
var calcA = days / 5;
var calcB = Math.floor(calcA); //Find No of weeks
//gs.print('No of Weeks is ' + calcB);
var calcC = Math.round((calcA - calcB) * 5); //Find our remainder days
//gs.print('No of Remaining Days is ' + calcC);
startDate.addWeeksLocalTime(calcB); //add Weeks first
//gs.print('More than 4 days - Week adjust first ' + startDate.getLocalDate());
startDate.addDaysLocalTime(calcC); //then the Days
//gs.print('More than 4 days - Day adjust ' + startDate.getLocalDate());
startDate.getDayOfWeekLocalTime();
if (startDate.getDayOfWeekLocalTime() == 6) {
startDate.addDaysLocalTime(2);
} else if (startDate.getDayOfWeekLocalTime() == 7) {
startDate.addDaysLocalTime(1);
}
startDate.getDayOfWeekLocalTime();
gs.print('More than 4 days ' + startDate.getLocalDate());
} else {
startDate.addDaysLocalTime(days);
startDate.getDayOfWeekLocalTime();
if (startDate.getDayOfWeekLocalTime() == 6) {
startDate.addDaysLocalTime(2);
} else if (startDate.getDayOfWeekLocalTime() == 7) {
startDate.addDaysLocalTime(1);
}
startDate.getDayOfWeekLocalTime();
gs.print('Less than 5 days ' + startDate.getLocalDate());
}
Adjusted to account for Saturday and Sunday end dates.
Based on Nootan's post, I searched some more and came up with this option that seems to work using Schedules as well:
BusDayCommv2();
function BusDayCommv2() {
var startDate = new GlideDateTime();
gs.print(startDate.getDisplayValue());
var days = 14;
var hours = 9; //number of hours in a day in the schedule
gs.print('No of days ' + days);
var dur = new GlideDuration(60 * 60 * hours * 1000 * days);
var schedule = new GlideSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //8-5 weekdays
var end = schedule.add(startDate, dur);
gs.print("End Date: " + end.getDisplayValue());
}
Sourced from the solution here: https://www.servicenow.com/community/developer-forum/end-date-should-be-current-date-and-addition-of...

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 01:28 AM
Hi @Ken Pruitt,
Use below code:
var dur = new DurationCalculator();
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //add sys id of 8-5 weekdays schedule.
dur.setStartDateTime(new GlideDateTime());
dur.calcDuration(324000); //durations in seconds.
gs.print("End date="+dur.getEndDateTime()+" Start Date="+new GlideDateTime());
got exact 10 Business days difference:
Let me know if it helped.
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 05:13 AM
Still a day short of the expected date:
*** Script: Start Date=2022-12-20 13:12:23 End date=2023-01-02 23:00:00
[0:00:00.010] Total Time

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2022 07:40 PM
Hi @Shane J,
Could you please share the image of the schedule?
I would like to know the week days included in the schedule.
In the script :
dur.calcDuration(324000); //durations in seconds.
I have used the seconds. Calculated as 3600*9*10.
Check your cmn_schedule and add the right sys_id and right seconds calculation:
dur.setSchedule('08fcd0830a0a0b2600079f56b1adb9ae'); //add sys id of 8-5 weekdays schedule.
Or else you can create a Schedule with 5 working days a week (can be 24*5) and then perform the seconds calculation.
Let me know if it helped you
Thanks
Nootan