Glide Schedule and TimeZone

rad2
Mega Sage

When using a schedule and calculating the duration between a start and end time in a timezone, it always considers UTC. even when using TimeZone when defining a GlideSchedule. Adding the script below

var sched = new GlideSchedule('qwdfghjuhytfgdsew3245etrygh');
var start = "2025-02-18 13:30:00"
var end = "2025-02-18 19:30:00"
var startTime = new GlideDateTime(start);
var endTime = new GlideDateTime(end);
sched.setTimeZone('Europe/Berlin');
gs.debug(sched.duration(startTime, endTime));
 
response is  00:30:00
But my schedule starts from 20:00:00pm to 23:59:59pm
7 REPLIES 7

Ankur Bawiskar
Tera Patron
Tera Patron

@rad2 

check this link and it talks about using PlannedTaskAPI to calculate the duration

Calculate the business duration using a schedule 

I ran the same script and in preferences set the timezone as GMT and it gave me the correct output

Note: Remember the timezone it takes is based on your user preference

try {
    var schedule = new GlideSchedule("08fcd0830a0a0b2600079f56b1adb9ae", );

    var open = new GlideDateTime('2025-01-03 07:00:00');
    var closed = new GlideDateTime('2025-01-03 09:19:00');

    // Use closed_at for "mark_closed" business rule and resolved_at for "mark_resolved" business rule 
    
var plannedTaskAPI = new SNC.PlannedTaskAPI();
var resp = plannedTaskAPI.calculateDuration(open.getDisplayValue(), closed.getDisplayValue(), '08fcd0830a0a0b2600079f56b1adb9ae');

var response = new JSON().decode(resp);
var duration = response.duration;
gs.info(duration);

} catch (ex) {
    gs.info(ex);
}

AnkurBawiskar_0-1739943736204.png

 

 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

@rad2 

Hope you are doing good.

Did my reply answer your question?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@rad2 

try this

var sched = new GlideSchedule('qwdfghjuhytfgdsew3245etrygh', 'Europe/Berlin');
var start = "2025-02-18 13:30:00"
var end = "2025-02-18 19:30:00"
var startTime = new GlideDateTime(start);
var endTime = new GlideDateTime(end);
gs.info(sched.duration(startTime, endTime));

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader