Convert UTC to Time Zone (US/Eastern) from GlideRecord query output
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2015 07:08 AM
I'm relatively new to ServiceNow development and have encountered an issue that seems very simple but has thwarted me, thus far. I've referenced:
Re: How to convert GMT date to ESTand other entries, but haven't been able to figure it out yet.
In a workflow script, there's a GlideRecord query (called, "sysapproval") that pulls some change request variables that are sent to an OnBase approval engine in an XML payload. The fields, "current.sysapproval.start_date" and "current.sysapproval.end_date" are currently output in UTC. Times need to be converted to system time (US/Eastern) or via the java time zone utilities: ("America/New_York"). Because these times aren't actually displayed in ServiceNow, they don't get automatically converted to the system time zone (US/Eastern).
I've tried to do this time zone conversion within the payload function via the GlideDateTime setTZ(tz) method: GlideDateTime - ServiceNow Wiki
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");
var ps_dt = new GlideDateTime(current.sysapproval.start_date);
var plannedStartDt = ps_dt.setTZ(tz);
var pe_dt = new GlideDateTime(current.sysapproval.end_date);
var plannedEndDt = pe_dt.setTZ(tz);
However, when I do this, the resulting values in the XML get set to "undefined" and the approval engine chokes on the payload:
<Property><Name>Planned Start</Name><Value>undefined</Value></Property>
<Property><Name>Planned End</Name><Value>undefined</Value></Property>
I've tried to do this just about every alternative way I can think of but haven't had any success. What am I missing?
Thanks,
Keith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2015 12:12 AM
Are we doing this on a UI Page?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-09-2015 10:21 AM
Hi Peter,
You're definitely very kind to keep helping me out!
No, this isn't on a UI page, it's in a script that generates an XML payload that gets sent to OnBase (approval engine).
I was not able to solve this issue in a timely manner and so I've passed it on to a contracting company that does ServiceNow development for my employer. I plan on posting the result here once it gets solved. I believe that the solution should be rather simple and it was just due to my inexperience with SN development that caused this issue.
Thanks!
Keith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-27-2015 06:13 PM
Hi Keith,
I've run across some issues with this and found that I had to do this in order to get the right time to be displayed in either all UTC format or all EST format.
function getLocalDate() {
var gdt = new GlideDateTime(); // Without input sets it to current UTC.
//gs.log(gdt); *** Script: 2015-02-28 02:08:41 (UTC).
var timeZoneOffSet = gdt.getTZOffset();
//gs.log(timeZoneOffSet); *** Script: -18000000 (milliseconds). -5 Hours offset (EST)
gdt.setNumericValue(gdt.getNumericValue() + timeZoneOffSet);
//gs.log(gdt); *** Script: 2015-02-27 21:08:41 (EST).
return gdt;
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-08-2015 11:33 AM
Just did this successfully. I tried to provide all the necessary comments. Hope this helps! If it's what you were looking for can you mark is as correct answer? Thanks!
// Get date from service now
var time = new GlideDateTime(current.u_start_date);
// Display date retrieved
gs.print('GMT Time: '+time);
// Set timezone
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");
time.setTZ(tz);
// Get offset of timezone set above
var timeZoneOffSet = time.getTZOffset();
// Add offset to current time
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
// Print date/time in desired timezone
gs.print('Eastern time: '+time);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-20-2015 08:49 AM
Keep in mind, since this post, I believe the updated GlideDateTime functions in Eureka/Fuji have greatly made date/time functions and calculations much easier since they have added a bunch of Local date/time specific functions.