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-16-2022 11:45 AM
@Shane J1 it is definitely because of the offset from UTC. The closest I got was to use getDayOfWeekUTC() but my requirements changed and I only ended up needing to count calendar days without regards to weekends so addDays() ended up being my path forward.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2022 12:16 PM
That's pretty much what we're doing for now because our results trying to use schedules are all over the place. I've been tasked with figuring out why that's the case though, instead of the straight-up addDays workaround. Wish me luck I guess. 😉
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2022 12:19 PM
haha good luck! The only idea i had before my requirements change was probably overkill but I was going to include logic that accounted for each timezone on the User[sys_user] timezone column. That way regardless of who was using the form, there would/should be logic to return expected results.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-16-2022 12:29 PM
Given it all of 2 min thought but maybe this could come in handy.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-19-2022 06:27 AM - edited 12-19-2022 10:53 AM
I had kind of an epiphany in regard to this and as far as I can tell it's working.
Now it doesn't take ServiceNow Schedules or Holidays into account (I suppose you could do Holidays if you had someplace to reference them or hard code them in the script).
The basic thought is, find each day up until your days max, convert them to their Day of Week and dump that into an array. Determine how many weekend days, and add that to your previous max days (with some other adjustments).
I've tested with a few days, 45 days, and 50 days, and it's come out with the expected date each time for me.
Take a look if you're interested (I've got this running as a Fix Script):
scrubbed the code while I continue to test it...