Function to parse gs.getSession().getTimeZone();

G24
Kilo Sage

Hello.  Does someone have a function to parse the ZoneInfo array (?) or whatever it is that is returned from:

 

var userTimeZone = gs.getSession().getTimeZone();

 

 

 

The output from that looks like this:

 

sun.util.calendar.ZoneInfo[id="US/Eastern",offset=-18000000,dstSavings=3600000,useDaylight=true,transitions=235,lastRule=java.util.SimpleTimeZone[id=US/Eastern,offset=-18000000,dstSavings=3600000,useDaylight=true,startYear=0,startMode=3,startMonth=2,startDay=8,startDayOfWeek=1,startTime=7200000,startTimeMode=0,endMode=3,endMonth=10,endDay=1,endDayOfWeek=1,endTime=7200000,endTimeMode=0]]

 

I care about the id, "US/Eastern" in this example...

and the "offset", -18000000 in this example.

 

*** OR ***

Is there a better way to get the name/id and the offset?

I know that I can get the name with a different function:

var userTimeZoneName = session.getTimeZoneName();

But I don't know the best way to get the offset.  Thanks.

1 ACCEPTED SOLUTION

karthiknagaramu
Kilo Sage

Hi,

 

Using string manipulation we can get the offset. But might not be a best way.

 

var userTimeZone = gs.getSession().getTimeZone()+'';
gs.print(userTimeZone);
userTimeZone = userTimeZone.replace('sun.util.calendar.ZoneInfo','');
gs.print(userTimeZone);
var tmArr = [];
tmArr = userTimeZone.split(",");
gs.print(tmArr[1]);
var offset = tmArr[1]+'';
offset = offset.replace('offset=','');
gs.print(offset);

 

Regards,

Karthik Nagaramu

View solution in original post

11 REPLIES 11

Ankur Bawiskar
Tera Patron
Tera Patron

@G24 

what's your business requirement?

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

The business requirement was to get the Offset, to know how far ahead or behind the user is in Time.

 

Why do you ask though?  Is this object not something that should typically be used?

karthiknagaramu
Kilo Sage

Hi,

 

Using string manipulation we can get the offset. But might not be a best way.

 

var userTimeZone = gs.getSession().getTimeZone()+'';
gs.print(userTimeZone);
userTimeZone = userTimeZone.replace('sun.util.calendar.ZoneInfo','');
gs.print(userTimeZone);
var tmArr = [];
tmArr = userTimeZone.split(",");
gs.print(tmArr[1]);
var offset = tmArr[1]+'';
offset = offset.replace('offset=','');
gs.print(offset);

 

Regards,

Karthik Nagaramu

Final poor man's version:

//Note:  This TimeZone object may then be used directly in the GlideDateTime setTZ() function.  There may be no need to parse it.

//Get TimeeZone object from session.
var userTimeZone = gs.getSession().getTimeZone() + '';

//Strip off weird string before the start of the array.
userTimeZone = userTimeZone.replace("sun.util.calendar.ZoneInfo[", "");
//Strip off trailing two right brackets.
userTimeZone = userTimeZone.substr(0, userTimeZone.length - 2);

//Split the remaining string into array elements, using , as the delimiter.
var tmArr = [];
tmArr = userTimeZone.split(",");

//Get the id, the first element, and strip off double quotes.
var id = tmArr[0] + "";
id = id.replaceAll('"', "");

//Get the offset, the second element, and strip off the term "offset=".
var offset = tmArr[1] + '';
offset = offset.replace('offset=' , '');