Get Date back in time with elapsed time using a schedule

Edvin-Karalius
Tera Guru

Couldn't find a way to post an article to community. So let's pretend this is an article and not a question 🙂

Wrote a simple script to use when you need to get a date back in time using a schedule. (not possible ootb).
This would be typically used with something like (auto resolve incident after 3 days from last update).
And you would need to know "what is 3 days back in time, using a schedule"
So like. maybe you need a date back in time but ignoring weekdays/holidays.

Here is the script:

 

 

function getDateBackInTime(days, schedule, startDate){
  if (typeof schedule === 'string')
  	schedule = new GlideSchedule(schedule);
  
  if(!startDate)
    startDate = new GlideDateTime();
  
  startDate.addDays(-1);
  var diff = schedule.duration(startDate, new GlideDateTime());
  return diff.getDayPart() >= days ? startDate : getDateBackInTime(days, schedule, startDate);
};


getDateBackInTime(3, 'cmn_schedule sys_id HERE');

 

 

 

 

Here is an example or me running a weekday schedule, that ignores weekends.
Start date was today(Wednesday) and the result 3 workdays back is a Friday.

This can be used then in GlideRecord queries to auto-close tickets that have elapsed time in days based on schedule.

EdvinKaralius_0-1692821983536.png


Note: When using a schedule that is not 24 hours/day.
The elapsed time is much shorter. so a 8-17 schedule would be 9hours/day.
Setting a setting for 1 days would mean 27 hours of elapsed time and that would be a minimum of 3 days back in real time.
So i'd say avoid using this with schedules that are not 24/h day. Unless you are specifically looking for total elapsed time.
Even then this check in chunks of 1 day at a time. So it wont be 100% accurate.

0 REPLIES 0