IT CanvassTalk to an advisor
ServiceNow References · LessonBy , ServiceNow Trainer, 8 yrs · Published · ServiceNow · all levels

GlideDateTime API

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

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.

Practice challenge

+0 XPStreak ×0
Question 1 of 2
What does the GlideDateTime string constructor assume?

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.
Already working on ServiceNow and stuck on a live ticket?Get an expert ServiceNow developer on screen-share to finish your daily tasks with you. Deliver on time, protect your reputation and your job. Monthly support only, no task-wise plans.Task assigned · no idea where to startStill stuck · your job on the lineExpert joins your screenDelivered on timeExplore On Job Support