GlideDate object always returns UTC as Timezone???

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 05:27 PM
I see the documentation for the GlideDate object (NOT to be confused with GlideDateTime),
The writeup for setDisplayValue() says:
"Sets a date value using the current user's display format and time zone."
My time zone is US/Eastern. But after setting the Date using setDisplayValue(), when I then query the GlideDate() object for my timezone, using the getByFormat() function, it tells me that the timezone is UTC. This seems wrong. What am I missing?
Code:
var userTimeZone = gs.getSession().getTimeZone();
gs.addInfoMessage(userTimeZone);
var userDateFormat = gs.getSession().getUser().getDateFormat();
gs.addInfoMessage(userDateFormat);
var myMagicalDateString = "30.06.2023";
gs.addInfoMessage(myMagicalDateString);
var myGlideDateObject = new GlideDate();
//gs.addInfoMessage("myGlideDateObject: " + myGlideDateObject);
myGlideDateObject.setDisplayValue(myMagicalDateString);
gs.addInfoMessage("myGlideDateObject: " + myGlideDateObject);
var userTZ = myGlideDateObject.getByFormat("z");
gs.addInfoMessage("userTZ: " + userTZ);
Output:
Please help. Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-29-2023 05:38 PM
Your code is correct, and you're not missing anything. Here's what's happening step by step:
You set the user's time zone to US/Eastern.
You create a GlideDate object and set its value using setDisplayValue(). The value is stored in UTC.
When you call getByFormat("z"), it returns the time zone formatted according to the user's time zone setting, but the underlying value is still in UTC.
If you want to obtain the actual UTC time zone offset, you can use the getTZOffset() function:
var userTimeZone = gs.getSession().getTimeZone();
gs.addInfoMessage(userTimeZone);
var userDateFormat = gs.getSession().getUser().getDateFormat();
gs.addInfoMessage(userDateFormat);
var myMagicalDateString = "30.06.2023";
gs.addInfoMessage(myMagicalDateString);
var myGlideDateObject = new GlideDate();
myGlideDateObject.setDisplayValue(myMagicalDateString);
gs.addInfoMessage("myGlideDateObject: " + myGlideDateObject);
var userTZOffset = myGlideDateObject.getTZOffset();
gs.addInfoMessage("userTZOffset: " + userTZOffset);
The getTZOffset() function returns the time zone offset in minutes, which you can then convert as needed. Just remember that the underlying value stored in the GlideDate object is still in UTC.
In general, ServiceNow's GlideDateTime and GlideDate objects are designed to handle date and time values in a standardized way, taking into account user time zones for display purposes but storing values in UTC to ensure consistency across different time zones.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 06:11 AM - edited 08-30-2023 06:12 AM
@Harish Bainsla Here is another example where I set the date TWO ways, once using setValue() and once using setDisplayValue(). And I get UTC back both times...
var myGlideDate = new GlideDate();
myGlideDate.setValue("2023-06-30");
gs.addInfoMessage("myGlideDate after setValue() function: " + myGlideDate);
var userTZOffset = myGlideDate.getTZOffset();
gs.addInfoMessage("userTZOffset after setValue() function: " + userTZOffset);
var dateByFormat = myGlideDate.getByFormat("E--y--M--d--z--a");
gs.addInfoMessage("dateByFormat after setValue() function: " + dateByFormat);
var userTimeZone = gs.getSession().getTimeZone();
gs.addInfoMessage("Uzer TZ is Eastern Time as confirmed here: " + userTimeZone);
var userDateFormat = gs.getSession().getUser().getDateFormat();
gs.addInfoMessage("User Date Format: " + userDateFormat);
var testDate = "30.06.2023";
gs.addInfoMessage("testDate in User Date Format: " + testDate);
myGlideDate = new GlideDate();
myGlideDate.setDisplayValue(testDate);
gs.addInfoMessage("myGlideDate after setDisplayValue() function: " + myGlideDate);
userTZOffset = myGlideDate.getTZOffset();
gs.addInfoMessage("userTZOffset after setDisplayValue() function: " + userTZOffset);
dateByFormat = myGlideDate.getByFormat("E--y--M--d--z--a");
gs.addInfoMessage("dateByFormat after setDisplayValue() function: " + dateByFormat);
Output:
Questions:
1) It seems to me that the GlideDate() object HAS NO IDEA what the user's Timezone is. In other words, it does not STORE the user's timezone inside the object anywhere. Is that correct?
2) If that IS correct, then why the heck is there a function on the GlideDate() object for getting the user's Timezone??? Argh!!!
3) If that is NOT correct, then why is there a function for setting the value that supposedly takes into account the user's Timezone, called setDisplayValue()? Again, the documentation says "Sets a date value using the current user's display format and time zone." Is that true?? Does it really do anything differently based on my timezone when I use setDisplayValue()???
4) I find it extremely confusing that when I ask for the Timezone one way, using function getTZOffset(), I get a different and conflicting result than if I ask for it another way, using function getByFormat(). That seems WRONG!
I am still extremely confused. This is a nightmare.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-04-2025 05:21 AM - edited 06-04-2025 05:49 AM
Hey, just in case you haven't figured this out yet, or someone is stumbling upon this post from a search engine:
I only know how it works for GlideDateTime, but I would assume it's the same for GlideDate.
All you're doing with .setDisplayValue() is telling the system what it needs to do to calculate the UTC date, which it then stores. So for example, if your timezone is UTC+2, and you use .setDisplayValue(), ServiceNow substracts two hours to get to the UTC time, which it then stores.
So, .setValue() assumes that you're passing the date in UTC, and .setDisplayValue() assumes that you're passing the date in your own timezone. Both methods actually result in data being stored in UTC (without any information about the user's TZ), but for .setDisplayValue(), ServiceNow has to calculate the time difference between your TZ and UTC.