- 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:04 AM
how to get only the time part 'hh:mm:ss' format for subtract?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2025 12:29 AM
Hi @Chris45
24:00:00 will be converted to the next day (next date 00:00:00) by default. Nowhere we can see the time as 24:00:00.
The methods are working as expected
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2025 12:12 AM
and getByFormat('HH:MM:SS') prints different yet still unexpected result
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-31-2025 12:12 AM
on the line:
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'));
You're subtracting the same time from itself, which results in a duration of 0. The same formatting issue happens again: you're using a 12-hour format and it will show 12:00:00 for a 0 duration because of how the getByFormat('hh:mm:ss') method works.
To get the correct 24-hour format, use the getByFormat() method but specify a 24-hour format instead of 12-hour format.
You can achieve that by using HH instead of hh.
Use the following updated code:
gs.info("dur: " + dur.getByFormat('HH:mm:ss')); // 24-hour format