This article covers everything you need to know about
GlideDuration
, from core functions to advanced use cases like triggering events, reminders, and automations.
πΉ What is GlideDuration
?
GlideDuration
is a ServiceNow class used to represent and manipulate spans of time, such as:
- “2 hours”
- “1 day and 30 minutes”
- “15 seconds”
Unlike GlideDateTime
, which is about specific moments, GlideDuration
is about how long something lasts.
π§± How GlideDuration Works Internally
GlideDuration stores a duration as a difference in time from the base reference:
1970-01-01 00:00:00
So if a duration is “4 Days 8 Hours”, its internal value is:
1970-01-05 08:00:00
This enables consistent time math and formatting.
π§ͺ Creating GlideDuration Instances
// From a display string
var dur1 = new GlideDuration('3 12:00:00');
// From milliseconds
var dur2 = new GlideDuration(60000); // 1 minute
// From another duration (clone)
var dur3 = new GlideDuration(dur1);
// Set value manually
dur3.setDisplayValue('2 04:00:00');
π GlideDuration Method Cheat Sheet
Hereβs a detailed breakdown of each important method with examples:
π¦ getDisplayValue()
Returns: Human-readable duration string
Format: "n Days n Hours n Minutes"
var dur = new GlideDuration('3 12:00:00');
gs.info(dur.getDisplayValue()); // "3 Days 12 Hours"
π¦ getDurationValue()
Returns: Raw duration in d HH:mm:ss
format
Example:
var dur = new GlideDuration(60000);
gs.info(dur.getDurationValue()); // "00:01:00"
π¦ getDayPart()
Returns: Only the number of full days in the duration
var dur = new GlideDuration('3 12:00:00');
gs.info(dur.getDayPart()); // 3
π¦ getRoundedDayPart()
Returns: Days, rounded up if time > 12 hours
var dur = new GlideDuration('3 14:00:00');
gs.info(dur.getRoundedDayPart()); // 4
π¦ getValue()
Returns: Internal datetime value in YYYY-MM-DD HH:mm:ss
(How far from Jan 1, 1970)
var dur = new GlideDuration('3 12:00:00');
gs.info(dur.getValue()); // "1970-01-04 12:00:00"
π¦ setValue(Object o)
Sets the internal value using a datetime string
var dur = new GlideDuration();
dur.setValue('1970-01-05 08:00:00');
gs.info(dur.getDisplayValue()); // "4 Days 8 Hours"
π¦ setDisplayValue(String asDisplayed)
Sets duration directly using string input
var dur = new GlideDuration();
dur.setDisplayValue('3 08:00:00');
gs.info(dur.getDisplayValue()); // "3 Days 8 Hours"
π¦ add(GlideDuration duration)
Returns: New GlideDuration = sum of two durations
var d1 = new GlideDuration('3 12:00:00');
var d2 = new GlideDuration('3:00:00');
var total = d1.add(d2);
gs.info(total.getDisplayValue()); // "3 Days 15 Hours"
π¦ subtract(GlideDuration duration)
Returns: New GlideDuration = difference between two durations
var d1 = new GlideDuration('3 12:00:00');
var d2 = new GlideDuration('3:00:00');
var result = d1.subtract(d2);
gs.info(result.getDisplayValue()); // "3 Days 9 Hours"
π¦ getByFormat(String format)
Returns: Duration formatted in a custom way
var dur = new GlideDuration('3 22:00:00');
gs.info(dur.getByFormat('HH:mm')); // "22:00"
π¨ Using GlideDuration
to Trigger Events
While GlideDuration
itself doesn’t fire events, itβs perfect for creating time-based conditions to manually trigger events, like:
- Reminders
- Auto-escalations
- SLA breach notifications
π Example 1: Auto-Close After 3 Days Inactivity
use below code in scheduled job which runs daily
var gr = new GlideRecord('incident');
gr.addQuery('state', '!=', 'closed');
gr.query();
while (gr.next()) {
var updated = new GlideDateTime(gr.getValue('sys_updated_on'));
var now = new GlideDateTime();
var diff = GlideDateTime.subtract(now, updated);
var threeDays = new GlideDuration('3 00:00:00');
if (diff.compareTo(threeDays) >= 0) {
gr.state = 'closed';
gr.close_notes = 'Auto-closed due to 3 days of inactivity';
gr.update();
}
}
π§ Tips & Tricks
π‘ Tip | π¬ Description |
---|---|
Use GlideDuration(60000) | Create 1-minute duration from ms |
compareTo() | Returns 0 , <0 , or >0 β great for conditions |
getByFormat() | Clean display (HH:mm, etc.) |
Use in Scheduled Jobs | Ideal for time-based logic checks |
Combine with gs.eventQueue() | Best way to trigger time-based events |
β Common Mistakes
Mistake | Fix |
---|---|
Using strings for math | Always use GlideDuration objects |
Forgetting day in format | Format must be d HH:mm:ss |
Confusing with GlideDateTime | One is a time, the other is a duration |
π§Ύ Summary Table
Function | Description |
---|---|
getDisplayValue() | “2 Days 5 Hours” |
getDurationValue() | “2 05:00:00” |
getValue() | Internal datetime (based on 1970) |
add() / subtract() | Add or subtract durations |
compareTo() | Compare two durations |
getDayPart() | Only the day count |
setValue() / setDisplayValue() | Set duration manually |
π§© Ready-to-Use Pattern
var now = new GlideDateTime();
var past = new GlideDateTime(task.sys_updated_on);
var gap = GlideDateTime.subtract(now, past);
var wait = new GlideDuration('1 00:00:00');
if (gap.compareTo(wait) >= 0) {
gs.eventQueue('custom.event', task, task.sys_id, task.assigned_to);
}
π§ Final Thoughts
GlideDuration
isnβt just a utility β itβs your best friend when building anything time-related