- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 07:51 PM
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~~
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 10:12 PM - edited 05-22-2024 10:15 PM
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:
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 08:15 PM
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_...
Please mark this answer as helpful and correct if helped.
Kind Regards,
Ravi Chandra
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 08:35 PM
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!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 08:40 PM
The property also defines default timezone for an instance
Please refer:
https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0714577
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-22-2024 08:58 PM
@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
Then i change it to other timezone. As below pictures the result doesn't change.
