Convert UTC to EST using GlideRecord

divya321
Kilo Contributor

We have a requirement where I need to get the Scheduled start and end dates from email body which is in UTC format(both past and future dates will be available in email body)and convert this to EST. After converting it into EST we need to capture it on Planned start and end dates fields on change request.

I tried below code in my Inbound action

var startdt= email.body.scheduled_start;
var gdt = new GlideDateTime(startdt);
gdt.addSeconds(-14400);

var enddt= email.body.scheduled_end;
var gdt1 = new GlideDateTime(enddt);
gdt1.addSeconds(-14400);
//gs.log("date end _2 "+gdt1.getValue());

current.start_date = gdt.getValue();
current.end_date= gdt1.getValue();

Above code works fine for now but I have to change the seconds in code (gdt.addSeconds(-14400);) twice in an year because EST has daylight saving.

Could anyone help me what code needs be placed instead of updating the seconds manually in the code during day light saving.

 

Thanks,

Divya Saridey

 

 

1 ACCEPTED SOLUTION

Hello Try this

 

var startdt = new GlideDateTime(email.body.scheduled_start);
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");
startdt.setTZ(tz);
var timeZoneOffSet = startdt.getTZOffset();
startdt.setNumericValue(startdt.getNumericValue() + timeZoneOffSet);

View solution in original post

5 REPLIES 5

Omkar Mone
Mega Sage

Hi 

Check this GlideDateTime in docs, you need to use the UTC functions - 

https://developer.servicenow.com/app.do#!/api_doc?v=newyork&id=r_GDT-GlideDateTime

 

Regards,

Omkar Mone

divya321
Kilo Contributor

Hi Omkar,

 

This is the format in email body:

Scheduled Start:  07/16/2019 02:00:00 (UTC)

 

I have written the below code in my inbound action:

var startdt= email.body.scheduled_start;

var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");

var time = new GlideDateTime(startdt);

time.setTZ(tz);

Even though I am converting UTC time to EST its giving me the output as 2019-07-16 02:00:00. So, here it is not converting it to EST timezone.

 

Thanks,

Divya Saridey

Hello Try this

 

var startdt = new GlideDateTime(email.body.scheduled_start);
var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");
startdt.setTZ(tz);
var timeZoneOffSet = startdt.getTZOffset();
startdt.setNumericValue(startdt.getNumericValue() + timeZoneOffSet);

divya321
Kilo Contributor

Thanks Rajesh it worked for me.

I have changed the last line of your code as below:

var startdt = new GlideDateTime(email.body.scheduled_start);

var tz = Packages.java.util.TimeZone.getTimeZone("America/New_York");
startdt.setTZ(tz);
var timeZoneOffSet = startdt.getTZOffset();
startdt.setNumericValue(startdt.getNumericValue() + timeZoneOffSet); //Changed this line

 

Thanks,

Divya