Different time zone issue in scripting

saba tanveer
Tera Contributor

User timezone on instance is africa/johannesburg and I am working from Pakistan so when I am trying to write script to get current time it is giving me africa/johannesburg time. Is there any way to get the time where system operates automatically without mentioning timezone.

 

my code:

var g = new GlideDateTime();
gs.info(g.getDisplayValue());
9 REPLIES 9

scott barnard1
Kilo Sage

Hi Saba

Given the joburg time is +2

Try

 

var gdt = new GlideDateTime();
var gtime1 = new GlideTime();
gtime1.setValue("02:00:00");
gdt.add(gtime1);
gs.print(gdt);

 

ctime.png

This code is giving me above result and in my clock time is 15:42

 

Hi Saba

That's right.

var gdt = new GlideDateTime(); gives you the time at GMT
The rest then adds 2 hours to it. I add 2 hours because the timezone offset for Johannesburg is +2 
So the final value is the time in Johannesburg. If you want the local time in Lahore you would use +5 etc
 
 
docs are here
 
Regards

 

 

 

 

Community Alums
Not applicable

Hi @saba tanveer ,

 

You can use the below code & convert to PST

 

 

convertTimetoPST: function(dat){        
//convert date to PST timezone
        var time = new GlideDateTime(dat);
        var targetTimezone = 'PST'; // ensure you give correct timezone Abbreviation
        var tz = Packages.java.util.TimeZone.getTimeZone(targetTimezone);
        time.setTZ(tz);
        var timeZoneOffSet = time.getTZOffset();
        time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
        return time;
},

 

 

 If my answer helped you in any way, please mark it as helpful or correct.