GlideDateTime Sanity Check

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-30-2023 10:06 AM
Is there a GlideDateTime expert who can give me (and all of us) a sanity check on the GlideDateTime functions??
I had to create a spreadsheet to understand all the different functions:
"Internal" means what is used by ServiceNow itself, regardless of custom settings or system properties or user settings.
The "internal" DateTime format is always yyyy-MM-dd HH:mm:ss. This is hard-coded. You can depend on it.
The "internal" timezone for GlideDateTime objects is always UTC, also known as GMT. It's hard-coded. Count on it.
ServiceNow uses the word "Local" to mean "from the perspective of the current user's TIMEZONE".
"System" means whatever your instance system properties are set to. If the user has not customized his settings, he defaults to the system settings.
Formats may be either Internal or User. Timezones may be either System or User. This gives 4 combinations.
When displaying GlideDateTime values, you may want the Date and Time, the Date only, or the Time only, so 3 combinations.
For the examples above, User Date Format is dd.MM.yyyy and User Time Format is hh:mm:ss a.
Here is my getDateOnly function:
function getDateOnly(anyGlideDateTime){
var sDateAndTime = anyGlideDateTime.getDisplayValue();
var sTimeOnly = anyGlideDateTime.getUserFormattedLocalTime();
var iCharactersToKeep = sDateAndTime.length() - sTimeOnly.length() - 1;
var result = sDateAndTime.substring(0, iCharactersToKeep);
return result;
}
And here is my (possibly useless) getUserTime() function:
function getUserTime(anyGlideDateTime){
var userDateFormat = gs.getSession().getUser().getDateFormat();
var result = anyGlideDateTime.getTime().getByFormat(userDateFormat) + " " + anyGlideDateTime.getUserFormattedLocalTime();
return result;
}
Thoughts? Questions? Criticism? Would love to hear it. Thank you.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-31-2023 03:35 PM
This is quite a read. I do things differently though. I know enough for myself, but not to teach since dates/times are not my favorite topic and I consult the developer site all the time. I also don't usually have to be conscientious of user format, but I've had to before.
My date only for the current user looks like this:
function userDateOnly(dateTimeValue) {
var gdt = new GlideDateTime(dateTimeValue);
var userGDT = gdt.getDisplayValue();
var userDate = new GlideDateTime(userGDT).getDate();
return userDate;
}
My time only for the current user is even simpler:
function userTimeOnly(dateTimeValue) {
var gdt = new GlideDateTime(dateTimeValue);
var userTime = gdt.getUserFormattedLocalTime();
return userTime;
}
Date & Time is just as simple:
function userDateTime(dateTimeValue) {
var gdt = new GlideDateTime(dateTimeValue);
var userGDT = gdt.getDisplayValue();
return userGDT;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-04-2023 09:49 AM
Hi, @Claude DAmico . Thanks for responding. What you referenced is the ServiceNow Library Reference. The developer site is here: https://developer.servicenow.com/dev.do
For your "userDateOnly" function, you could just use myGDT.getLocalDate()
Or maybe I'm not understanding what kind of values you are passing INTO that function. I would need to see it in context since JavaScript is only loosely typed.
Same with your "userTimeOnly()" function. How is that different than just myGDT.getUserFormattedLocalTime() ? Thanks.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-05-2023 03:22 PM
The URL is still the developer site but to the Reference section. That's not too important here though.
I thought about turning my script snippets into functions as an afterthought just to kind of match what you are doing. For me, the end user's date/time is unimportant a lot of the time so I don't really deal with what is displayed often at all.
IF I need to treat it as important, these functions would ideally accept "yyyy-mm-dd hh:mm:ss" for GlideDateTime's input format and return per the user's configuration. I suppose I only half-thought through the whole thing (unfortunate end-of-day fog...), but:
- getDisplayValue() is documented to return in the current user's display format and time zone
- I would prefer this for UI-specific things like UI Pages, alerts, messages, etc. where formatting is important because it is going to be displayed to end users outside of the context of a field
- getLocalDate() is documented to return in the standard yyyy-MM-dd format and in the current user's time zone
- I would use this if it wasn't important to honor the end user's preferences for formatting but still important for timezone since this would be displayed to the end user outside of the context of a field.
- If I'm setting the value of a date field, I would need to determine if the date should be set from the perspective of the end user (getLocalDate), system (getDate), or specific location/timezone (rmConvertTimeZone Script Include) which I have had to do before
It really depends on what exactly I'm doing with dates and/or times. Sorry if this muddies the waters. Like I said, not a big fan of dates/times. I almost always feel I am doing something different every time I have to work with them. 😅