Obtaining datetime values based on a specific timezone

jonathan_long1
Giga Contributor

I am having datetime calculation issues with the following requirement and cannot seem to find the appropriate methods to
make this determination, here is the use case:

Use Case:   An incident is created @ 13:00 EST with a location reference set to "Los Angeles" and that location record
has the   timezone field set to "US/Pacific".

Question: In a business rule how can you work with the incidents "created" datetime stamp and
obtain the time format for a particular timezone (in this case using the location's timezone field)   and compare that time to see if it's before 12N local time in LA?

Note:   using "getDisplayValue" on a GlideDateTime object simply returns the time in the
timezone that the user logged in is using, getvalue returns GMT, the timezoneoffset method gives offset based on user's tz,

I needed to be able to specify a timezone and have it return the correct time format for that timezone or how somehow get the appropriate offset.

How can I accurately determine this?

Thanks for any assistance!

Jonathan

3 REPLIES 3

johnolivermendo
ServiceNow Employee
ServiceNow Employee

Hey Jonathan,



Have you tried using setTZ() from the GlideDateTime object?



GIven a string value of a timezone from the Location table I would perform the following script:



var gr = new GlideRecord('cmn_location');


//gr.addQuery(ADD YOUR QUERY HERE);


gr.query();


if (gr.next()) {


        var tz = Packages.java.util.TimeZone.getTimeZone(gr.time_zone);


        var time = new GlideDateTime();


        time.setTZ(tz);


        time.setValue(gr.sys_created_on);


}




Once you set the specific GlideDateTime object to your desired timezone and set the time based on your sys_created_on value, you can make your comparison.


Nate23
Mega Guru

This post has a good example by Rahul Pandey: Hi , I want convert on timezone to another timezone .



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



line 4: you can use the locations timezone. something like       var tz = Packages.java.util.TimeZone.getTimeZone(current.location.timezone);


ccajohnson
Kilo Sage

What I have discovered is that all date/time fields are actually stored as GMT. I had a client that wanted to check to see if a given date/time was within a specific blackout period that was defined in a specific time zone. I needed to create a way of setting the date/time from GMT to the specific time zone to accomplish this. Here is the function I wrote that could help you in converting the date stored in the field to whatever timezone you need for your other calculations:


function setToTimeZone(dateTime, iTz, oTz) {


      var inputFmt = 'yyyy-MM-dd HH:mm:ss';


      var inputTz = iTz;


      var input = new Packages.java.text.SimpleDateFormat(inputFmt);


      var tzI = new Packages.java.util.TimeZone.getTimeZone(inputTz);


      input.setTimeZone(tzI);


      dateTime = input.parse(dateTime);


      outputFmt = 'yyyy-MM-dd HH:mm:ss';


      var outputTz = oTz;


      var output = new Packages.java.text.SimpleDateFormat(outputFmt);


      var tzO = new Packages.java.util.TimeZone.getTimeZone(outputTz);


      output.setTimeZone(tzO);


      return output.format(dateTime);


}



The inputs for the the function are as follows:


dateTime is the raw date from the field, iTz is the input timezone (for our needs, it will always be 'GMT'), oTz is the expected output timezone (for your example it will be 'US/Pacific').



It will return the date/time in the specified format.