This article is providing the way to calculate the duration of days by using Scheduler function in Script.
With Scheduler, it is easy to calculate the duration with some option, like exclude week-end or holiday.
You can calculate the duration of 2 days in the next step:
- Open Scheduler ("System Scheduler -> Schedules -> Schedules")
- Open the schedule you want to use. In this example, using "8-5 weekdays" to calculate exclude weekend.
- Check the option : All Day. If this is not checked, the calculation is consider only business hour set in the scheduler.
- Prepare the script. Please refer the below sample.
If you need to calculate exclude holiday, please use the schedule: "8-5 weekdays excluding holidays"
var startDate = new GlideDateTime('2020-4-17 00:00:00');
var endDate = new GlideDateTime('2020-4-21 00:00:00');
var schedule = new GlideSchedule();
schedule.load('08fcd0830a0a0b2600079f56b1adb9ae'); // loads "8-5 weekdays" schedule. ('08fcd0830a0a0b2600079f56b1adb9ae' is sysid of the schedule.)
var duration = schedule.duration(startDate, endDate);
gs.print(duration.getDurationValue()); // gets the elapsed time in schedule
Result of the code