From c8f303f785cd1cab283257174ed5a5302eaed70a Mon Sep 17 00:00:00 2001 From: Code with bhav <92107107+bhavyaa30@users.noreply.github.com> Date: Tue, 7 Oct 2025 12:32:40 +0530 Subject: [PATCH 1/2] script.js --- .../Convert UTC Time To Local Time/script.js | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Core ServiceNow APIs/GlideDateTime/Convert UTC Time To Local Time/script.js diff --git a/Core ServiceNow APIs/GlideDateTime/Convert UTC Time To Local Time/script.js b/Core ServiceNow APIs/GlideDateTime/Convert UTC Time To Local Time/script.js new file mode 100644 index 0000000000..ae875178a8 --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Convert UTC Time To Local Time/script.js @@ -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.'); + } +})(); From c7b59e5914563f7ba4f2f71825fa3a9279052532 Mon Sep 17 00:00:00 2001 From: Code with bhav <92107107+bhavyaa30@users.noreply.github.com> Date: Tue, 7 Oct 2025 12:33:39 +0530 Subject: [PATCH 2/2] readme.md --- .../Convert UTC Time To Local Time/readme.md | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Core ServiceNow APIs/GlideDateTime/Convert UTC Time To Local Time/readme.md diff --git a/Core ServiceNow APIs/GlideDateTime/Convert UTC Time To Local Time/readme.md b/Core ServiceNow APIs/GlideDateTime/Convert UTC Time To Local Time/readme.md new file mode 100644 index 0000000000..f4ccab6387 --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Convert UTC Time To Local Time/readme.md @@ -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