Prasad Dhumal
Mega Sage
Mega Sage

🧭 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.

Comments
Roger11
Giga Guru

I think this best explains most date/time issues.
var myDate = new GlideDateTime();
gs.print(myDate);
gs.print(myDate.getDisplayValue());
gs.print(myDate.getDate() + ' β€”(SYSTEM DATE)');
gs.print(myDate.getLocalDate() + ' β€”(LOCAL DATE)\n\n');

*** Script: 2025-08-21 23:44:56
*** Script: 22-08-2025 09:44:56
*** Script: 2025-08-21 ---(SYSTEM DATE) *** Script: 2025-08-22 ---(LOCAL DATE)

 

Version history
Last update:
β€Ž07-01-2025 12:11 AM
Updated by:
Contributors