The CreatorCon Call for Content is officially open! Get started here.

Next to the datetime fields we are displaying the current logged in user time zone details but its

Susilpa
Kilo Expert

Hi All,

Good Day.

 

We have the functionality we need to show the time zone details of the current logged user next to the date & time fields, to achieve this we have implemented one UI Macro as suggested by SNOW HI Team and this macro were added to the attributes list of the date & time fields.

 

Susilpa_6-1759737507611.png

 

 

The issue here if we chosen the time zone on the user profile as "Etc/GMT+12" but its showing this like "Etc/GMT-12" next to the date & time fields on the change form.

Susilpa_0-1759737107501.png

 
 
 

Susilpa_4-1759737256954.png

 

UI Macro & its script:

Susilpa_7-1759737670153.png

 

 

Script Include Script:

var TZTimezoneUtils = Class.create();
TZTimezoneUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getTimezone: function() {
            var tzNameMap = {
            "Asia/Bangkok": "ICST",
            "Asia/Jakarta": "WIST",
            "Asia/Manila": "PDT",
            "Asia/Phnom_Penh": "ICST",
            "Asia/Saigon": "ICST",
            "Asia/Taipei": "CDT",
            "Australia/North": "ACDT",
            "Australia/South": "ACDT",
            "BST": "BDST",
            "Canada/Atlantic": "ADT",
            "Canada/Central": "CDT",
            "Canada/Eastern": "EDT",
            "Canada/Mountain": "MDT",
            "Canada/Newfoundland": "NDT",
            "Canada/Pacific": "PDT",
            "CST": "CDT",
            "EST": "EDT",
            "Etc/GMT+12:00": "GMT-12:00",
            "Etc/GMT-3:00": "GMT+03:00",
            "Etc/GMT-7:00": "GMT+07:00",
            "Europe/London": "BST",
            "GMT": "GMT",
            "Hongkong": "HKST",
            "IST": "IDT",
            "JST": "JDT",
            "PST": "PDT",
            "Singapore": "SGST",
            "US/Arizona": "MDT",
            "US/Central": "CDT",
            "US/Eastern": "EDT",
            "US/Hawaii": "HDT",
            "US/Mountain": "MDT",
            "US/Pacific": "PDT"
        };
       
        var tzName = gs.getSession().getTimeZoneName();
        if ((new GlideDateTime()).isDST() && tzNameMap[tzName] !== undefined){
            var zone = tzNameMap[tzName];
            return zone;
        }
        else
            return gs.getSession().getTimeZoneShortName();
    },
   

    type: 'TZTimezoneUtils'
});
 
Can you please help me on the above issue, if any small help its really appreciate it.
 
Regards,
Kotaiah Sadari 

 

 

3 REPLIES 3

G Ponsekar
Giga Guru

Hi @Susilpa ,

 

Please refer KB article: https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB1580453#:~:text=Etc/GMT+%2D%2....

 

The reason your timezone shows up as Etc/GMT-12 instead of Etc/GMT+12 is due to a standard time zone convention known as the POSIX format, which reverses the signs for GMT offsets. 
  • In the ISO 8601 standard, a positive offset means you add time to GMT.
  • In the POSIX standard, the convention is the opposite: the sign indicates the direction from GMT, so a positive value represents a time zone west of GMT and a negative value represents a time zone east of GMT. 
Since your user profile uses Etc/GMT+12, this is interpreted in the POSIX format as 12 hours west of GMT, which is equivalent to GMT-12. Your Script Include is correctly returning the session's timezone name, which adheres to this standard. 
 
How to fix the UI Macro
To correct the display, you need to adjust your Script Include to perform a sign reversal specifically for the "Etc/GMT" timezones. 
 
Updated Script Include (TZTimezoneUtils)
Modify your existing Script Include to handle the "Etc/GMT" format separately. The updated code will check for "Etc/GMT" in the timezone name and flip the sign if found.
 
javascript
var TZTimezoneUtils = Class.create();
TZTimezoneUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
    getTimezone: function() {
        var tzNameMap = {
            "Asia/Bangkok": "ICST",
            "Asia/Jakarta": "WIST",
            "Asia/Manila": "PDT",
            "Asia/Phnom_Penh": "ICST",
            "Asia/Saigon": "ICST",
            "Asia/Taipei": "CDT",
            "Australia/North": "ACDT",
            "Australia/South": "ACDT",
            "BST": "BDST",
            "Canada/Atlantic": "ADT",
            "Canada/Central": "CDT",
            "Canada/Eastern": "EDT",
            "Canada/Mountain": "MDT",
            "Canada/Newfoundland": "NDT",
            "Canada/Pacific": "PDT",
            "CST": "CDT",
            "EST": "EDT",
            "Etc/GMT+12": "GMT+12", // Corrected value
            "Etc/GMT+11": "GMT+11",
            "Etc/GMT-3": "GMT-03", // Corrected value
            "Etc/GMT-7": "GMT-07", // Corrected value
            "Europe/London": "BST",
            "GMT": "GMT",
            "Hongkong": "HKST",
            "IST": "IDT",
            "JST": "JDT",
            "PST": "PDT",
            "Singapore": "SGST",
            "US/Arizona": "MDT",
            "US/Central": "CDT",
            "US/Eastern": "EDT",
            "US/Hawaii": "HDT",
            "US/Mountain": "MDT",
            "US/Pacific": "PDT"        };
       
        var tzName = gs.getSession().getTimeZoneName();
        
        // Handle Etc/GMT format
        if (tzName.startsWith("Etc/GMT")) {
            var offsetStr = tzName.substring(7); // Remove 'Etc/GMT'
            var offset = parseInt(offsetStr);
            if (!isNaN(offset)) {
                // Flip the sign and format correctly
                var sign = (offset > 0) ? '-' : '+';
                var hour = Math.abs(offset);
                return 'GMT' + sign + (hour < 10 ? '0' : '') + hour;
            }
        }
        
        // Handle Daylight Saving Time lookups
        if ((new GlideDateTime()).isDST() && tzNameMap[tzName] !== undefined){
            return tzNameMap[tzName];
        }
        
        // Fallback to standard short name
        return gs.getSession().getTimeZoneShortName();
    },
   
    type: 'TZTimezoneUtils'});

 

If I could help you with your Query then, please hit the Thumb Icon and mark as Correct !!

 

Thanks, GP

-O-
Kilo Patron

What is the need for this complication? The time zone is the one the user selected in its profile. So all you need to do to display the time zone name is to copy field "Time zone" from the current user's profile. As for the offset, that can be obtained by running (new GlideDateTime()).getTZOffset() / 1000 / 60 / 60.

In fact both values are fixed and only change if the user changes the setting on their profile. Given that, I don't even understand why this is needed. Users should know what time zone they have selected for themselves.

Ankur Bawiskar
Tera Patron
Tera Patron

@Susilpa 

what's the business requirement for this?

if you said the code is given by ServiceNow team then does it mean they gave half-working code?

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader