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-05-2015 07:17 AM
You need to set the timezone before you set the date.
So
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");
var time = new GlideDateTime();
time.setTZ(tz);
time.setValue(current.sysapproval.end_date);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2015 07:42 AM
Hi Peter,
Thanks for your quick and helpful response. I tried your suggestion like this:
//set time zone
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");
//set start date
var ps_dt = new GlideDateTime();
ps_dt.setTZ(tz);
ps_dt.setValue(current.sysapproval.start_date);
//set end date
var pe_dt = new GlideDateTime();
pe_dt.setTZ(tz);
pe_dt.setValue(current.sysapproval.end_date);
The payload didn't cause the approval engine to crash, but the times were still in UTC rather than EST.
<Property><Name>Planned Start</Name><Value>2015-02-05 17:00:00</Value></Property>
<Property><Name>Planned End</Name><Value>2015-02-05 18:00:00</Value></Property>
The start_date should be converted from 17:00 to 12:00 and end date should be converted from 18:00 to 13:00.
So, clearly there's a step that I'm still missing. Any other ideas?
Thanks,
Keith
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-05-2015 11:49 PM
Right, understand it now.
When working with date/times, you should work in UTC. This will ensure all calculations are correct.
When displaying the time, this is when you would convert it to the users time zone. To get this to work, we need to use "getDisplayValue()"; So it should be something like:
var ps_dt = new GlideDateTime();
gs.print(ps_dt); //The current time
//set time zone
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");
//Set the timezone
ps_dt.setTZ(tz);
//Display timezone value
gs.print(ps_dt.getDisplayValue());
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2015 10:38 AM
Hi Peter,
Thanks so much for replying to my post again. I tried your suggestion, but it keeps spitting back the date/time in UTC.
In my testing, I learned that if I use gs.print(); or gs.print() when building the XML payload, it causes an error.
Here's what I've tried:
//create new glide datetime objects with dates from the gliderecord query
var ps_dt = new GlideDateTime(current.sysapproval.start_date);
var pe_dt = new GlideDateTime(current.sysapproval.end_date);
//define time zone
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");
//set time zone in variables
ps_dt.setTZ(tz);
pe_dt.setTZ(tz);
Then, in the XML I output the dates like this, but it still outputs UTC:
payload += '<Property><Name>Planned Start</Name><Value>' + ps_dt.getDisplayValue() + '</Value></Property>\n';
payload += '<Property><Name>Planned End</Name><Value>' + pe_dt.getDisplayValue() + '</Value></Property>\n';
I've also tried using .getValue(), which also outputs UTC:
payload += '<Property><Name>Planned Start</Name><Value>' + ps_dt.getValue() + '</Value></Property>\n';
payload += '<Property><Name>Planned End</Name><Value>' + pe_dt.getValue() + '</Value></Property>\n';
Also, I find it less than helpful that the setTZ() documentation, GlideDateTime - ServiceNow Wiki, shows you how *set* the time zone but it doesn't provide information on how to *output* it!
Thanks,
Keith