π Mastering Date and Time Handling in ServiceNow: Tips, Scripts, and Best Practices
New article articles in ServiceNow Community
Β·
Jul 01, 2025
Β·
article
π§ Introduction
Date and time handling is one of the most common β and sometimes confusing β tasks in ServiceNow development. Whether youβre calculating SLA breaches, setting scheduled jobs, or customizing business rules, understanding how ServiceNow processes date/time values is critical for accuracy and consistency across the platform.
π§© How Dates Work in ServiceNow
ServiceNow stores all dates in UTC format in the database, but displays them in the userβs time zone.
- GlideDateTime β used for both date and time (e.g.,
2025-07-01 14:25:00) - GlideDate β stores only the date portion (e.g.,
2025-07-01) - Time zones matter: Always convert to/from user-specific zones where needed
π§ Common Use Cases & Scripts
1. Get Current DateTime
var gdt = new GlideDateTime(); gs.info("Current DateTime: " + gdt.getDisplayValue());
π’ Output (example):
Current DateTime: 2025-07-01 13:45:00
2. Add/Subtract Time
var gdt = new GlideDateTime(); gdt.addDaysLocalTime(7); // Adds 7 days gs.info("One week later: " + gdt.getDisplayValue());
π’ Output (if today is 2025-07-01):
One week later: 2025-07-08 13:45:00
3. Difference Between Two Dates
var start = new GlideDateTime('2025-06-25 10:00:00'); var end = new GlideDateTime('2025-07-01 14:00:00'); var diff = GlideDateTime.subtract(start, end); gs.info("Diff in milliseconds: " + diff.getNumericValue()); gs.info("Diff in days: " + (diff.getNumericValue() / 86400000));
π’ Output:
Diff in milliseconds: 532800000
Diff in days: 6.47
4. Convert Date String to GlideDateTime
var str = "2025-07-01 12:30:00"; var gdt = new GlideDateTime(str); gs.info("Converted: " + gdt.getDisplayValue());
π’ Output:
Converted: 2025-07-01 12:30:00
5. Compare Dates
var start = new GlideDateTime('2025-06-25 10:00:00'); var end = new GlideDateTime('2025-07-01 14:00:00'); if (start.before(end)) { gs.info("Start is before end"); } else { gs.info("Start is not before end"); }
π’ Output:
Start is before end
| Problem | Cause | Tip |
|---|---|---|
| Wrong date in reports | Time zone not considered | Always use GlideDateTime for reporting |
| SLA misfires | Improper business schedule used | Attach SLAs to correct schedules |
| Manual date manipulation errors | String-based operations | Always use GlideDate or GlideDateTime objects |
β Best Practices
- Prefer GlideDateTime over JavaScript native
Date()for accuracy and consistency - Use
getValue()for database values,getDisplayValue()for user-friendly display - Always test date logic in different time zones
- Leverage scheduled jobs and Event Queue for time-based automation
- Avoid hardcoding time offsets (e.g.,
+5:30), rely on system settings instead
π― Conclusion
Working with date and time in ServiceNow requires more than just knowing the syntax β it demands an understanding of time zones, data types, and platform behaviors. With the right techniques, you can build robust, time-aware solutions that drive accuracy and consistency in your applications.
https://www.servicenow.com/community/developer-articles/mastering-date-and-time-handling-in-servicenow-tips-scripts-and/ta-p/3305664