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

Chris45
ServiceNow Employee
ServiceNow Employee

thanks for the reply, while 

dur.getByFormat('HH:mm:ss') prints '00:00:00' for both input?

00:00:00

'

@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));

 

Chris45
ServiceNow Employee
ServiceNow Employee

yes, that's what I did, cannot use the getDisplayValue or getByformat to get expected, thanks all

@Chris45 

Please check my below response.

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

@Chris45 

The GlideDateTime.subtract method returns a GlideDuration object, which represents the difference between two date-time values. However, the getByFormat('hh:mm:ss') method formats the duration in a 12-hour clock format, which is why you're seeing 12:00:00 instead of 24:00:00 or 00:00:00.

getByFormat time in 24 hours format 

GlideTime - Scoped 

To fix this, you can use getDisplayValue()

Script:

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

var dur_seconds = GlideDateTime.subtract(new GlideDateTime("2025-03-28 18:04:55"), new GlideDateTime("2025-03-28 18:04:55"));
gs.info("dur: " + dur_seconds.getDisplayValue());

Output:

AnkurBawiskar_0-1743405633891.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