Want to convert GMT to EST/PST by passing timezone

Uday Tej
Giga Contributor

Want to  convert GMT to  EST/PST   by  passing timezone. Can you please help  me how can we do that in service now.

12 REPLIES 12

Uday Tej
Giga Contributor

Hi Paremsh,

 

 I have tried below  

 

var tz = Packages.java.util.TimeZone.getTimeZone('America/New_York'); //specify the time zone here
var gdt = new GlideDateTime();
gdt.setTZ(tz);

var convertedTime = gdt.getDisplayValue();

gs.info("convertedTime" + convertedTime);

im getting  output as 

convertedTime 2021-02-08 05:54:11

BUt I want to get output as
convertedTime 2021-02-08 05:54:1 AM EST without any  hard coding EST  by passing America/New_York'. To  get automatically  as EST.

Can you please tell me how can i do it.


Thanks,
Uday

@Uday Tej 

can you try this

I have set the timezone in variable and then used it

var timezone = 'EST';

var time = new GlideDateTime();
gs.info('GMT time is->'+time);
var tz = Packages.java.util.TimeZone.getTimeZone(timezone);
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
gs.info('EST time is->'+time);


var hours = time.getDayOfMonthUTC();

if(hours < 13){
time = time + ' AM ' + timezone;
}
else{
time = time + ' PM ' + timezone;
}

gs.info(time);

Regards
Ankur

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

I dont think it is possible. You are passing a timezone, how the system will understand it have to be converted it into EST? there is no relation b/w  America/New_York and EST, both are same timezones.