Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## Overview
This script converts a UTC date/time field in ServiceNow to the local time of the user that the script runs under using GlideDateTime.
This distinction is important in certain contexts, such as asynchronous business rules, scheduled jobs, or background scripts, where the executing user may differ from the record owner.
It is useful for notifications, reports, dashboards, or any situation where users need localized timestamps that reflect the correct timezone.

## Table and Field Example
Table: incident
Field: opened_at (stored in UTC)

## How It Works
The script queries the incident table for the most recent active incident.
Retrieves the opened_at field (in UTC).
Creates a GlideDateTime object to convert this UTC timestamp into the local time of the executing user.
Logs both the original UTC time and the converted local time.

## Key Notes
Conversion is always based on the timezone of the user executing the script.
In asynchronous operations (background scripts, scheduled jobs, async business rules), this is the system user running the script.

## Reference
https://developer.servicenow.com/dev.do#!/reference/api/zurich/server_legacy/c_GlideDateTimeAPI
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(function() {
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
gr.orderByDesc('opened_at');
gr.setLimit(1); // Example: take the latest active incident
gr.query();

if (gr.next()) {
// GlideDateTime object from UTC field
var utcDateTime = gr.opened_at;

// Convert to user's local time zone
var localTime = new GlideDateTime(utcDateTime);
var displayValue = localTime.getDisplayValue(); // Returns local time in user's timezone

gs.info('UTC Time: ' + utcDateTime);
gs.info('Local Time: ' + displayValue);
} else {
gs.info('No active incidents found.');
}
})();
Loading