How to convert date and time into another time zone in scoped application

Poorna7
Tera Expert

In below script include we are getting the location time zone and date of the location to be in office . Currently My Requirement is to convert the both location date(ld) and current datetime (nowDT) into location time zone i.e. getLOcationTimeZone . Both ld and now DT are in GMT time zone.

var getDate = Class.create();
getDate.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getDateInfo: function() {

var getLocation = current.variables.office_location.time_zone; // Get the LOcation Time Zone
var locationDate = this.getParameter('sysparm_user_sysId'); // Get the Date of the Location on which the user wants to be in office
var ld = new GlideDateTime(locationDate); // GMT Time Zone
var nowDT = new GlideDateTime(); //GMT Time Zone
var dur = GlideDateTime.subtract(nowDT,ld);
var duration = dur.getNumericValue();
var durationSeconds = (duration/1000);

11 REPLIES 11

Abdul Azeez
Mega Guru

Hi Akansha,

 

Did you tried using getUserTimezone() ?

 

https://developer.servicenow.com/dev.do#!/reference/api/paris/server/no-namespace/GlideCalendarDateTimeAPI#GCDT-getUserTimeZone?navFilter=timezone

getUserTimezone() retrieves the time zone for the current user session.

But I want the time zone to be converted into location time zone.

The user time zone and location timezone can be different.

Upender Kumar
Mega Sage

try below

var time = new GlideDateTime();     // Display date retrieved       
gs.print('GMT Time: '+time);     // Set timezone     
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York"); 
//var tz = Packages.java.util.TimeZone.getTimeZone("IST");
 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);

Hi

I'm not the OP, but have same question.

In a Scoped Application I get

"JavaException: java.lang.SecurityException: Use of Packages calls is not permitted in scoped applications"