diff --git a/Core ServiceNow APIs/GlideDateTime/Convert UTC to Local Time/readme.md b/Core ServiceNow APIs/GlideDateTime/Convert UTC to Local Time/readme.md new file mode 100644 index 0000000000..9a027de385 --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Convert UTC to Local Time/readme.md @@ -0,0 +1,7 @@ +## Overview +This script converts a UTC date/time field in ServiceNow to the **user's local time** using **GlideDateTime**. +Useful for notifications, reports, dashboards, or any situation where users need **localized timestamps**. + +## Table and Field Example +- **Table:** `incident` +- **Field:** `opened_at` (stored in UTC) diff --git a/Core ServiceNow APIs/GlideDateTime/Convert UTC to Local Time/script.js b/Core ServiceNow APIs/GlideDateTime/Convert UTC to Local Time/script.js new file mode 100644 index 0000000000..ae875178a8 --- /dev/null +++ b/Core ServiceNow APIs/GlideDateTime/Convert UTC 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.'); + } +})();