glide schedule duration does not give the correct result

AlejandraEs
Tera Contributor
hello could someone help me? I replicated this for a schedule from 9:00 am to 8:00 pm, as a result I get 10hrs.

if I change the end date to "2022-01-12 10:00:00" I get the same result
 
Does anyone know why this happens?
 
var schedule=new GlideSchedule('213fbc5b1b9878106bb2a937b04bcbf4', 'America/Mexico_City'); //my schedule L-V 9-20hrs



var duration = schedule.duration(new GlideDateTime("2022-01-11 16:00:00"),new GlideDateTime("2022-01-12 11:00:00"));//start  date, end date
 
gs.log("calcScheduleDuration with schedule: " + duration); 

 

gs.log(duration.getDurationValue()); //this will give you answer in duration

 

gs.log(duration.getNumericValue()/3600000); //this will give in you in hours
6 REPLIES 6

Adam Stout
ServiceNow Employee
ServiceNow Employee

I would debug the times you are creating to make sure the timezones are what you think they are.  My guess is that there is an extra shift and you aren't following into the window you are expecting.

Hitoshi Ozawa
Giga Sage
Giga Sage

Probably because "All day" is not checked in the schedule. When "All day" is not checked, duration calculates only on the hours of the schedule instead of the entire 24 hours per day.

Select the check box to make the schedule entry active for the entire duration.

https://docs.servicenow.com/bundle/orlando-it-service-management/page/administer/on-call-scheduling/...

AlejandraEs
Tera Contributor

thanks for your comments, but I continue with the problem, the function does not do the desired calculations when the second date has a time of 09:00:00, 10 and 13

Define schedule as below:

find_real_file.png

Script:

var schedule = new GlideSchedule('de7409e09742011086d3b4b3f153af2a', 'America/Mexico_City');
var duration = schedule.duration(new GlideDateTime("2022-01-11 16:00:00"),new GlideDateTime("2022-01-12 13:00:00"));
gs.log("calcScheduleDuration with schedule: " + duration); 
gs.log(duration.getDurationValue()); //this will give you answer in duration
gs.log(duration.getNumericValue()/3600000); //this will give in you in hours

Execution results:

1 End date/time is 2022-01 11:00:00

*** Script: calcScheduleDuration with schedule: 1970-01-01 19:00:00
*** Script: 19:00:00
*** Script: 19

2. End date/time is 2022-01 09:00:00

*** Script: calcScheduleDuration with schedule: 1970-01-01 17:00:00
*** Script: 17:00:00
*** Script: 17

 3. End date/time is 2022-01 10:00:00

*** Script: calcScheduleDuration with schedule: 1970-01-01 18:00:00
*** Script: 18:00:00
*** Script: 18

4. End date/time is 2022-01 13:00:00

*** Script: calcScheduleDuration with schedule: 1970-01-01 21:00:00
*** Script: 21:00:00
*** Script: 21