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 JobsIdeal for time-based logic checks
Combine with gs.eventQueue()Best way to trigger time-based events

❌ Common Mistakes

MistakeFix
Using strings for mathAlways use GlideDuration objects
Forgetting day in formatFormat must be d HH:mm:ss
Confusing with GlideDateTimeOne is a time, the other is a duration

🧾 Summary Table

FunctionDescription
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

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *