Development · LessonBy Neelima, ServiceNow Architect · Published · ServiceNow · all levels
GlideDateTime and dates
Store, compare and display dates correctly across timezones, and calculate durations that survive daylight saving.
Quick answer
Store, compare and display dates correctly across timezones, and calculate durations that survive daylight saving.
Key takeaways
- Values are stored in UTC and rendered per user
- Use GlideDateTime arithmetic, not string parsing
- Use a schedule when business hours matter
- GlideDuration display values are human readable, duration values are for maths
Storage versus display
Date/time fields are stored in UTC and displayed in the user timezone. Every bug in date handling starts with someone comparing a displayed string to a stored value.
Working with GlideDateTime
Use the API for arithmetic rather than parsing strings.
var start = new GlideDateTime(current.opened_at);
var end = new GlideDateTime();
var dur = GlideDateTime.subtract(start, end); // GlideDuration
gs.info(dur.getDisplayValue()); // 2 Days 4 Hours
gs.info(dur.getDurationValue());Business time
Elapsed time is rarely the answer for SLAs. Use a schedule so weekends and holidays are excluded.
- DurationCalculator applies a schedule and timezone
- Schedules live in cmn_schedule and can be reused by SLAs, on call and jobs
- Always state the timezone in report titles, it prevents a lot of arguments
Practice challenge
+0 XPStreak ×0
Question 1 of 2
In which timezone are date/time fields stored?
Frequently asked questions
Why does a date show a different day for one user?
Their profile timezone differs. The stored UTC value is the same, only the rendering changed.
How do I add two working days to a date?
Use DurationCalculator with a schedule. Adding 48 hours ignores weekends and holidays.