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

Community Alums
Not applicable

Hi David,

 

 I am facing similar issue, Were you able to find the solution?

 

Thanks,

Anu

@Community Alums 

can you post a new question for this and share the relevant scripts etc and tag me there?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Anu hi

 

I did get a solution - with the help of Servicenow Certification team.

 

We had to:

  • have a Global Script Include
  • deploy this within App as a "Fix Script"
  • Customer Admin has to create Global Script include (from Fix Script content) as a post install step.

Then within Script Include we are able to :

	getTimeFromConstraintWithDayNum: function(scheduled, dnum, tod, tzone) { 
		// get Scheduled time in intended timezone to 
		var time = new GlideDateTime(scheduled); 
		var tz = Packages.java.util.TimeZone.getTimeZone(tzone); 
		
		time.setTZ(tz); 
		time.addDaysLocalTime(dnum); 
		
		var d = time.getDisplayValue(); 
		var nd = d.substring(0, 11) + tod + ':00'; 
		var ntime = new GlideDateTime(); 
		
		ntime.setTZ(tz); 
		ntime.setDisplayValue(nd); 
		
		return ntime; 
	},

 

@David Hubbard 

Can you share how are you invoking it and with what parameters?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur

The Script Include needs to have "Accessible from: All application scopes"

 

Then call something like:

var tzutil = new global.My_App_TimeUtil();
var utcTime = tzutil.getTimeFromConstraintWithDayNum('2023-06-26 16:00:00', 1, '18:00', 'America/Los_Angeles');

which gives

"2023-06-28 01:00:00"

To clarify this function - In our usage - Day 0 is defined as the on the calendar day of that timezone, so Day = 1 is next day @18:00 in that timezone - this function returns a GlideDateTime which can be stored .