Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

garyopela
ServiceNow Employee
ServiceNow Employee

So i was needing to get the current date/time in a specific timezone the other day.

 

I know that you can easily generate it with the current user's timezone  with either of these:

gs.info(gs.nowDateTime());

gs.info(new GlideDateTime().getDisplayValue());
 
These can act weird, though, whenever you have a job running as a system account.

 

I know you can easily generate the current date in UTC with this:

gs.info(new GlideDateTime());
 
But telling it exactly which timezone to use was proving difficult. I've found many posts, such as the below:
 

Then I've found other posts where they were doing a lot of math to try and generate it, but I wanted to avoid that because it seemed like overkill.

 

We do have the following GlideDateTime functions available on the docs site: https://docs.servicenow.com/bundle/utah-api-reference/page/app-store/dev_portal/API_reference/GlideD...

 

  • GlideDateTime.setTZ(Timezone)

This has the following example:

var tz = gs.getSession().getTimeZone();
var gdt = new GlideDateTime(); 
gdt.setTZ(tz);
 
The problem here is that setTZ has to receive a TimeZone as its argument, so you cannot pass something easy, like 'US/Eastern,' you have to pass an actual TimeZone object. 

There is another/better option, and that's GlideDateTime().setTimeZone(), well, at least that's what I THOUGHT at first. I tried this code:
 
var gdt = new GlideDateTime();
gdt.setTimeZone('US/Eastern');
gs.info(gdt);
 
So, I thought, man, I have it. The problem here is that setTimeZone is what is called an 'undocumented' feature. Or at least if it's documented, I couldn't find any documentation on it. But, alas, when i ran that script, it returned UTC time AGAIN, even though I changed the timezone.
 
Anyways, after a couple of hours of trying a bunch of different things, I finally got it working, and then it made sense. So, the following code, are you ready for it???? the following code will return the current date/time in whatever timezone you want (lol, shouldn't be this hard):
 
var gdt = new GlideDateTime();
gdt.setTimeZone('US/Eastern');
gs.info(gdt.getDisplayValue());
The key here was getDisplayValue() which made sense once I thought about it. Anyways, this was a long post to try and explains something that seemed simple, but all of the solutions that I could find were WAY overworked.
 
Now, if you wanted to go one step further... You can pull down, through a GlideRecord query, a time and then specify which timezone you want it in. In the following script, I show how to get the timezone pulled back in UTC, in the current user's timezone, and in a specified timezone.
var gr = new GlideRecord('incident');
gr.addQuery('number', 'INC0402714');
gr.setLimit(1);
gr.query();

if (gr.next()){
    gs.info('Number: ' + gr.getValue('number'));
    gs.info("UTC Time: " + gr.getValue('sys_created_on'));
    gs.info("Curr User TZ: " + gr.getDisplayValue('sys_created_on'));
    
    var tmpTime = new GlideDateTime();
    tmpTime.setDisplayValue(gr.getDisplayValue('sys_created_on'));
    gs.print("UTC again: " + tmpTime);
    tmpTime.setTimeZone('US/Eastern');
    gs.print("US/Eastern TZ: " + tmpTime.getDisplayValue());
}
 
Hope this helps. If you guys can find any documentation around GlideDateTime().setTimeZone(), please link it below to make this more official. Also, if this has helped you, please give it a reaction. This really helps me understand what kind of content helps you guys out. For now, I usually just post small tidbits that I've found that I thought might help other people.
 
Thanks!
 
2 Comments