- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 12:02 AM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 01:49 AM
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 12:43 AM
thanks for the reply, while
dur.getByFormat('HH:mm:ss') prints '00:00:00' for both input?
00:00:00
'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 01:49 AM
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));
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 03:41 PM
yes, that's what I did, cannot use the getDisplayValue or getByformat to get expected, thanks all
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 02:02 AM
Please check my below response.
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-31-2025 12:23 AM
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
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:
If my response helped please mark it correct and close the thread so that it benefits future readers.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader