Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Hitoshi Ozawa
Giga Sage
Giga Sage

If time period is to be specified as below:

find_real_file.png

 

Note that "new GlideDateTime()" creates date in UTC. So if datetime is specified in New Mexico time, it is necessary to add +6 hours.

var schedule = new GlideSchedule('de7409e09742011086d3b4b3f153af2a', 'America/Mexico_City');

var gtTimeOffset = new GlideTime();
gtTimeOffset.setValue("06:00:00");

var startDateTime= new GlideDateTime("2022-01-11 09:00:00");
startDateTime.add(gtTimeOffset);
gs.info("startTime:" + startDateTime);

var endDateTime = new GlideDateTime("2022-01-12 11:00:00");
endDateTime .add(gtTimeOffset);
gs.info("endTime:" + endDateTime );

var duration = schedule.duration(startDateTime, endDateTime);
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-12 11:00:00

*** Script: startTime:2022-01-11 15:00:00
*** Script: endTime:2022-01-12 17:00:00
*** Script: calcScheduleDuration with schedule: 1970-01-01 13:00:00
*** Script: 13:00:00
*** Script: 13

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

*** Script: startTime:2022-01-11 15:00:00
*** Script: endTime:2022-01-12 15:00:00
*** Script: calcScheduleDuration with schedule: 1970-01-01 11:00:00
*** Script: 11:00:00
*** Script: 11

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

*** Script: startTime:2022-01-11 15:00:00
*** Script: endTime:2022-01-12 16:00:00
*** Script: calcScheduleDuration with schedule: 1970-01-01 12:00:00
*** Script: 12:00:00
*** Script: 12

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

*** Script: startTime:2022-01-11 15:00:00
*** Script: endTime:2022-01-12 19:00:00
*** Script: calcScheduleDuration with schedule: 1970-01-01 15:00:00
*** Script: 15:00:00
*** Script: 15

BTW, ServiceNow will convert the date/time entered into the form into UTC datetime in server so it would be necessary to use local date/time.