The CreatorCon Call for Content is officially open! Get started here.

Convert UTC to Time Zone (US/Eastern) from GlideRecord query output

keith_rokoske
Kilo Explorer

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

16 REPLIES 16

Are we doing this on a UI Page?


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


kungfuu72
Giga Expert

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;


}


tim_harris
Giga Expert

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);


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.