Different time zone issue in scripting

saba tanveer
Tera Contributor

User timezone on instance is africa/johannesburg and I am working from Pakistan so when I am trying to write script to get current time it is giving me africa/johannesburg time. Is there any way to get the time where system operates automatically without mentioning timezone.

 

my code:

var g = new GlideDateTime();
gs.info(g.getDisplayValue());
9 REPLIES 9

saba tanveer
Tera Contributor

Actually, I don't want to hardcode timezone. If I run it in Pakistan it should give correct result and if I run it in africa it should also give correct result. how to achieve that?

James Chun
Kilo Patron

Hi @saba tanveer,

 

Your script is correct, using the getDisplayValue() should always return the time of the current user's timezone.

 

Timezone set to Pacific/Auckland

JamesChun_0-1716592684540.png

 

Timezone set to US/Hawaii

JamesChun_1-1716592719123.png

 

Not sure how or where you ran the code, but your script works.

 

Cheers

 

 

Yes it is working but the issue is system timezone selected on instance is africa and I am working in Pakistan. I have a requirement to check if current time has passed or not so it is checking if africa current time has passed or not so,  it is creating issue while testing my code because testing team of Pakistan will see current time of Pakistan and Africa team will see current time of africa. I want some dynamic solution.

 

timezone.png

What do you mean it's causing an issue when you are testing it?

Can you elaborate a bit further on what the requirement was and what you have built?

Arpan Baishya
Kilo Sage

Hi @saba tanveer,

 

I think I found the solution to your problem. What you need to do is set the time zone for your user record to PST. In order to do so, please follow the steps mentioned in this article - Change the time zone list.

 

While doing so, you should see something like this - 

 

ArpanBaishya_1-1716650242041.png

 

Once you have moved PST to the list of selected time zones, hit Save.

 

Now, head to the User icon in the top right corner as shown below and click on Preferences.

ArpanBaishya_2-1716650452364.png

Next, click on Display and then you would see the dropdown menu for Time Zone. Select PST. That's it, your profile time zone has now been changed to PST while the system time zone remains as Africa/Johannesburg.

Now, you can run the script and you shall see your local time showing as the result. I tested your script with the IST value as can be seen below.

 

image.png


Hope this helped.