- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2022 04:43 PM
Hi there,
I am running into an issue that I can't quite seem to figure out at the moment and was hoping someone would have some insight to help.
I am working on a scoped application and I am trying to make some duration calculations based on a Start and End Date time. This Duration calculation also occurs during a schedule (9-5 Canada/Eastern).
The issue I am having is that the calculation for Duration using the Start (GlideDateTime) and End (GlideDateTime) values are using the getDisplayValueInternal() value for the calculation rather than the display value of these fields.
For example, when running the code below, I get the following output:
var endDateGDT = new GlideDateTime('2022-08-24 19:00:00');
gs.addInfoMessage('End Date Internal Display Value: ' + endDateGDT.getDisplayValueInternal());
My instance timezone is set to Canada/Eastern as is the timezone in my display preferences and User record. Any ideas what may be causing this and why the DisplayValueInternal is not in the Canada/Eastern Timezone?
Thanks,
Ethan
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2022 05:04 PM
GlideDateTime
expects the value/date as constructor parameter in the system TZ (usually UTC). Actually this is most confusingly documented, sometimes it is stated that it expects UTC other times that it expects system TZ (which is mostly UTC). For sure it is not the current user's TZ.
So unless the user running the Background script has the same TZ configured for itself as the system, it is expected that the output wold be different from the parameter given to GlideDateTime
.
Here's what the docs say:
The difference between getDisplayValueInternal() and getDisplayValue() is only the format (the former using the system format, the latter using the format selected by the current user).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2022 05:04 PM
GlideDateTime
expects the value/date as constructor parameter in the system TZ (usually UTC). Actually this is most confusingly documented, sometimes it is stated that it expects UTC other times that it expects system TZ (which is mostly UTC). For sure it is not the current user's TZ.
So unless the user running the Background script has the same TZ configured for itself as the system, it is expected that the output wold be different from the parameter given to GlideDateTime
.
Here's what the docs say:
The difference between getDisplayValueInternal() and getDisplayValue() is only the format (the former using the system format, the latter using the format selected by the current user).
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-10-2022 08:06 AM
Hey Janos,
Thank you for the input and insight, this helped quite a bit! I got another suggestion from a colleague, I decided that I would use the .setDisplayValueInternal() method and set a new GDT to the getDisplayValue() string from my correctly formatted user GlideDateTime display value. This means that I was able to pass a correct internal value GlideDateTime to the schedule duration I was trying to calculate.
function getNewTimeRequested(plannedStartDate, plannedEndDate, employeeSchedule) {
try {
var startDate = new GlideDateTime(), endDate = new GlideDateTime();
var sched = new GlideSchedule(employeeSchedule);
startDate.setDisplayValueInternal(plannedStartDate), endDate.setDisplayValueInternal(plannedEndDate);
return new GlideDuration((sched.duration(startDate, endDate).getDurationValue()));
} catch (ex) {
gs.error(ex);
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2022 07:56 AM
You're welcome! 🙂
Indeed using setDisplayValueInternal or setDisplayValue is the correct way to initialize a GlideDateTime instance if the date/time value is in the current user's TZ.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-11-2022 07:56 AM
By date/time value, I meant the initial date/time value. 😄