Skip to content
IT Canvass
ServiceNow References · Lesson

GlideDateTime API

Quick answer

The methods you actually use, with the timezone behaviour spelled out.

Key takeaways

  • Constructor parses as UTC, setDisplayValue parses as user time
  • subtract returns a GlideDuration, not a number
  • Use DurationCalculator when business hours matter
  • compareTo, before and after keep comparisons readable

Construction

new GlideDateTime() gives now in UTC. Passing a string parses it as UTC unless you use setDisplayValue, which parses in the user timezone. That single distinction causes most date bugs.

Common methods

Keep this list to hand.

  • getDisplayValue and getValue for user and internal representations
  • addSeconds, addDays and addMonths for arithmetic
  • compareTo for ordering, before and after for readability
  • getDayOfWeekLocalTime and getWeekOfYearLocalTime for reporting buckets
  • GlideDateTime.subtract returns a GlideDuration

Working with durations

GlideDuration display values look like 3 Days 4 Hours. Use getDurationValue for maths and getRoundedDayPart when you need whole days. For business hours, use DurationCalculator with a schedule instead of raw subtraction.

Want to learn this properly?

Our live, instructor-led ServiceNow Training covers this hands-on, with real projects and a certification path.

Check your understanding

  1. What does the GlideDateTime string constructor assume?

    • A. User timezone
    • B. UTC
    • C. Instance timezone
    • D. Server locale
    Show answer

    B. UTC

    String parsing is UTC unless you use setDisplayValue.

  2. Which returns a GlideDuration?

    • A. getValue
    • B. GlideDateTime.subtract
    • C. addDays
    • D. compareTo
    Show answer

    B. GlideDateTime.subtract

    subtract produces a duration between two date times.

Frequently asked questions

How do I get the start of today for the logged in user?

Build a GlideDateTime, call setDisplayValue with the user date and 00:00:00, which converts correctly into stored UTC.

Why is my duration one hour out twice a year?

Daylight saving. Use a schedule aware calculation rather than adding seconds.
CallWhatsAppEnquire