Where to change the system time zone ?

newhand
Mega Sage
 

Hi ALL!

 

I have a simple question about the timezone.

 

When I use the code below to show a date and time, in which timezone is it displayed (seems like UTC)?

Can we change the system timezone, and if so, where can we change it?

 

 

 

 

var gdt = new GlideDateTime()
gs.info(gdt)

 

 

 

glide.sys.default.tz is used to set the default timezone for users. It doesn't affect the code above.

Is my understanding correct?

 

Thank you~~

 

 

Please mark my answer as correct and helpful based on Impact.
1 ACCEPTED SOLUTION

Jim Coyne
Kilo Patron

A GlideDateTime object is always initialized in the UTC timezone.  The data in the database is always stored in UTC, regardless of the "system" timezone.

 

What you need to do is switch your GlideDateTime objects timezone to the appropriate timezone and then use "setDisplayValue()" to set your date/time.

 

Here's an example script so you can see what is happening:

 

//get the timezone object for "Asia/Tokyo"
var tz = Packages.java.util.TimeZone.getTimeZone("Asia/Tokyo");

//initialize a new date/time object
var gdt = new GlideDateTime();
//show the date/time in UTC (what it is stored as by default)
gs.info("Default UTC = " + gdt.getValue());
//show the date/time in my timezone, which is Canada/Eastern, currently 4 hours behind
gs.info("My timezone = " + gdt.getDisplayValue());  //notice the use of getDisplayValue()

//set the timezone to Asia/Tokyo
gdt.setTZ(tz);
//show the current time in Tokyo, which is currently 9 hours ahead
gs.info("Tokyo = " + gdt.getDisplayValue());

//set the date/time object to midnight on June 1, 2024 in the Tokyo timezone
gdt.setDisplayValue("2024-06-01 00:00:00");  //notice the use of setDisplayValue()
//show the new time in Tokyo
gs.info("new time in Tokyo = " + gdt.getDisplayValue());

//show the time in UTC
gs.info("new time in UTC = " + gdt.getValue());

//set the timezone back to Canada/Eastern
var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");
gdt.setTZ(tz);
gs.info("new time in Canada/Eastern = " + gdt.getDisplayValue());

 

 

Here are the results:

JimCoyne_0-1716441278697.png

 

getValue() will always retrieve the date/time in the UTC timezone.  getDisplayValue() will get it in the current timezone of the GDT object.  Same thing with setDisplayValue(), it uses the current timezone of the GDT object, which by default is the current users timezone.  You override it by using setTZ().

 

So in short, you need to do:

 

//get the timezone object for "Asia/Tokyo"
var tz = Packages.java.util.TimeZone.getTimeZone("Asia/Tokyo");

//initialize a new date/time object
var gdt = new GlideDateTime();
//set the timezone to Asia/Tokyo
gdt.setTZ(tz);
//set the date/time object to whatever time in the Tokyo timezone
gdt.setDisplayValue(yourDateTimeValue);

 

View solution in original post

8 REPLIES 8

Ravi Chandra_K
Kilo Patron
Kilo Patron

Hello @newhand 

System timezone can be changed from the property you mentioned.

The user cab overrided the timezone with their preference.

Please refer: https://docs.servicenow.com/bundle/washingtondc-platform-administration/page/administer/time/task/t_...

https://www.servicenow.com/community/in-other-news/demystifying-glidedatetime-and-timezones-also-con...

 

Please mark this answer as helpful and correct if helped.

Kind Regards,

Ravi Chandra 

newhand
Mega Sage

@Ravi Chandra_K 

Thank you for your reply.

However, this property only affect the users' timezones.

What I am searching for is how to change the system timezone.

 

scenario:   

When I create a new GlideDateTime object, I want it to be in a specific timezone, not UTC. This is because my system interacts with other systems, all of which operate in the Japanese timezone.

When I receive a datetime string from other systems, it is actually a datetime string in the Japanese timezone. I cannot easily insert it into my database; I need to write some scripts to convert it to a UTC datetime string (ServiceNow doesn't provide me with any out-of-the-box API to handle this).

Therefore, if I can change the system timezone to Asia/Tokyo, I can easily accomplish this just with a new GlideDateTime('xxxxxx').

 

Help!!

 

 

 

 

 

Please mark my answer as correct and helpful based on Impact.

Ravi Chandra_K
Kilo Patron
Kilo Patron

@newhand 

The property also defines default timezone for an instance

Please refer:

https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0714577

 

 

@Ravi Chandra_K 
Thank you for your reply。

I have read the document, and in fact it is still talking about how to affect the user timezone. As I tried below, it doesn't work at all

newhand_0-1716436182907.pngnewhand_1-1716436209254.png

 

Then  i change it to  other timezone. As below pictures  the result doesn't change.

newhand_2-1716436278910.pngnewhand_3-1716436382708.png

 

Please mark my answer as correct and helpful based on Impact.