SN Pro Tips

View Original

Handling TimeZones in ServiceNow (TimeZoneUtil)

Dealing with Time Zones in ServiceNow can be a real nightmare. The only documented way to set the time-zone of a GlideDateTime object, is the setTZ() method. Unfortunately, this method requires a "TimeZone" object be passed in, in order to set the time-zone.

What is a TimeZone object? It's an object that doesn't exist in JavaScript, and the only way to get hold of one using the ServiceNow API is to use gs.getSession().getTimeZone(), which just gets the current user's time-zone. If you want to know what time it is in a different time zone though, you're out of luck, chump! 

At least, you were. Sufficiently annoyed by this problem, I finally decided to write a Script Include to handle this for me, and now that I've been using (and testing) it for a while now, I'm comfortable publishing it for general consumption. Hopefully you'll find each method of the Script Include well documented, so I'll skip the usual explanation about what's going on in each function. If you want to know how it works, just have a look at the comments. 

Note: If you'd prefer that, even for well-documented scripts, I still provide a detailed explanation, leave a comment and let me know!

See this content in the original post

Example usage

See this content in the original post

You may simply copy and paste that into your instance, and you'll have a server-side utility for handling time-zones! And as for a client-side version, well here's a quick GlideAjax one for you: 

See this content in the original post

Example Usage

See this content in the original post

It's probably a good idea to add a "get specified time in specified time-zone" sort of function into the client-callable Script Include, but I didn't get around to it. Perhaps I'll update this article later once I get around to writing one. ^_^


Get the latest scripts in this public Gist


See this content in the original post