How to set a specific time in a specific time zone
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2017 09:24 AM
I thought others might find this helpful.
Let's say you need to a business rule to set a Date/Time field with a specific time (midnight) in a specific time zone (Central).
The problem is, any user (including the system administrator) might be running this business rule, so you could get very different results depending on which user and which time zone they are in.
Here is what I found works for any user and any time zone:
var myTimeZone = new GlideDateTime();
var tz = Packages.java.util.TimeZone.getTimeZone( "US/Central" );
var desiredTimeZone = new GlideDateTime();
desiredTimeZone.setTZ( tz );
var actualOpenedDateTime = new GlideDateTime( "2017-05-05 00:00:00" );
actualOpenedDateTime.add( myTimeZone.getTZOffset() - desiredTimeZone.getTZOffset() );
createIncident.opened_at = actualOpenedDateTime.getValue();
Now, if you look at that incident as a user in the US/Central time zone, it will show an "Opened" date/time of 2017-05-05 00:00:00, even if someone from a different timezone opened it.
- Labels:
-
Scripting and Coding
- 11,449 Views
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2017 09:44 AM
Hi MG,
I'm confused about your use-case. Could it be that you might be over-thinking this?
Let's say that a user in US Eastern time-zone opens an incident at his noon in July. Since EDT is GMT-4, ServiceNow would log the opened time to 16:00:00 00:00:00 GMT (internally).
Then a user in US Central time zone views the incident. Because ServiceNow localizes all DateTime fields for the user, it would use this US Central user's time-zone (GMT-5) to format the date. Applying the -5 to the 16:00:00 00:00:00 UTC date would yield a localized time of 11:00 AM for the opened by date time from the central-time user's perspective, which would be correct.
I have always trusted this automatic localization behavior in order to avoid having to deal withTZ complexities.
Regards,
Trey Carroll
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-20-2017 09:49 AM
Opening an incident was a bad example - my bad.
Let's say you have a table which has summary records in it. Each summary record in it has a Start Date/Time field and an End Date/Time field. Each summary record spans 30 minutes and is designed to track the number of opened incidents within that timeframe.
However, you want these summary records to generate with a Start Date/Time and End Date/Time in a particular time zone ("US/Central"), with the first record starting at midnight and the last record ending at midnight. You'd most likely have them generate in a scheduled job or script action, which will be run by the System Administrator (who's most likely in a different time zone that US/Central).
The basics of that script is kept - and you can create a summary record for each half-hour of the day (12 AM - 12:30 AM, 12:30 AM - 1 AM, etc.) with those Start Date/Time and End Date/Time in the US/Central timezone. (Any user who views them with a different timezone will just see slightly different times based on their time zone offset, which works as desired).
It's a fairly specific scenario you have to be in, in order for this to be helpful, but I thought I would post it as I struggled for a bit on how to make this happen.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-12-2019 12:05 PM
Thanks! This helped me a ton. I have used this code to create a global getTZOffset Script Include that lets you pass in a timezone string and will return the offset in milliseconds. I also made it accessible by all scopes, since this code will not work in a scoped application.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-06-2021 12:15 AM
Thanks also, this is very useful! I've an integration where access to guest Wi-Fi is required to be automated in sites around the world. So using this I should be able to calculate the UTC time when the timer needs to run for say 09:00 - 17:00 local time in any given country. Using https://www.timeanddate.com/worldclock/meetingtime.html helps me visual/verify this.