is it a bug? for GlideDateTime.subtract

Chris45
ServiceNow Employee
ServiceNow Employee

for below code

var dur = GlideDateTime.subtract(new GlideDateTime("2025-03-28 18:04:55"), new GlideDateTime("2025-03-29 18:04:55"));
gs.info("dur: " +dur.getByFormat('hh:mm:ss'));

dur_seconds = GlideDateTime.subtract(new GlideDateTime("2025-03-28 18:04:55"), new GlideDateTime("2025-03-28 18:04:55"));
gs.info("dur: "+dur.getByFormat('hh:mm:ss'));

 

expect first print 24:00:00, and second to be 00:00:00, but both print

12:00:00

 is this correct? confused

1 ACCEPTED SOLUTION

@Chris45 

ooh, can try with Convert the duration to seconds and then format manually.

var dur = GlideDateTime.subtract(new GlideDateTime("2025-03-28 18:04:55"), new GlideDateTime("2025-03-29 18:04:55"));
var totalSeconds = dur.getDurationValue(); 

var hours = Math.floor(totalSeconds / 3600); // Get hours
var minutes = Math.floor((totalSeconds % 3600) / 60); // minutes
var seconds = totalSeconds % 60; //seconds

gs.info("dur: " + (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds));

 

View solution in original post

10 REPLIES 10

J Siva
Tera Sage

Hi @Chris45 
You need to modify your script as below.

var dur = GlideDateTime.subtract(new GlideDateTime("2025-03-28 18:04:55"), new GlideDateTime("2025-03-29 18:04:55"));
gs.info(dur.getDisplayValue());
gs.info(dur.getByFormat('hh:mm:ss'));


var dur_seconds = GlideDateTime.subtract(new GlideDateTime("2025-03-28 18:04:55"), new GlideDateTime("2025-03-28 18:04:55"));
gs.info(dur_seconds.getDisplayValue());
gs.info(dur_seconds.getByFormat('hh:mm:ss'));

Output:

JSiva_0-1743405657465.png

 

Explanation:
Since you are using "hh:mm:ss", the system will gives you the output in 12 hours format.
if you convert 24 hours -> 12 hours format, then the output should be 12 because in 12 hours format a day starts at 12 and ends at 12.

The same applies for 0 seconds, in 12 hours format, 12 will be consdierd as 0 as well as 24.

Hope this helps.

Regards,
Siva