ServiceNow References · LessonReviewed by Sneha I, ServiceNow Trainer, 8 yrs · Updated · Published · ServiceNow · all levels
ServiceNow 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.
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.