- 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 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 11:08 PM
@Jim Coyne
Thank you !!
The point is:
By default setDisplayValue will use the user's timezone to apply the datetime string.
But if we use setTz before the setDisplayValue method, it will use the timezone I set to apply the datetime string instead of the user's timezone!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2024 12:12 PM
If you change the system timezone in System Basic Configuration, is there an negative effects that need to be taken into consideration from the data perspective? and what is the difference between America/Los Angeles and US/Pacific?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-15-2025 12:24 AM
If you are using workflow, use getDisplayValue('date_time_field') along with a 5-second timer before running this script. Regardless of the user's timezone, the workflow retrieves the date and time in the system timezone
