calculate Duration and Business Duration in scoped application

Sneha_18
Tera Contributor
Sneha_18_1-1735900877749.png

 

 
Sneha_18_0-1735900811208.png

Why the business duration is resulting in 0 seconds where it should be 1 hour 19 minutes as I have used OOB 

8-5 weekdays excluding holidays schedule where the start time as 8. 
Thanks

 

1 ACCEPTED SOLUTION

@Sneha_18 

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

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-1735914605128.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

View solution in original post

11 REPLIES 11

@Sneha_18 

why to use timezone in calculation?

You might be having timezone for your schedule already?

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

@Ankur Bawiskar , there is no timezone selected for the OOB schedule and it is floating and if we don't need to provide the timezone still the business duration is resulting 0 seconds when the dates fall under schedule time. If I pass the userTZ as GMT it is working fine but for other scenerio it doesn't seem to be working. Am I missing something? 

 

if (current.closed_at.nil()) { 
var opened = new GlideDateTime(current.opened_at);
var closed = new GlideDateTime();
var userTZ = gs.getSession().getTimeZoneName();
var gsBusiness = new GlideSchedule("090eecae0a0a0b260077e1dfa71da828",userTZ );
var durBusiness = gsBusiness.duration(opened, closed);
gs.info("Dur " + durBusiness);
current.business_duration = durBusiness;
}

 

 

@Sneha_18 

did you try with timezone as GMT?

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

@Sneha_18 

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

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-1735914605128.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

Hi @Ankur Bawiskar , this is working fine for me, thank you so much.