Make sure time is in US/Central time in the business rule

karanpreet
Tera Guru

Hello,

I've been searching on the forums and elsewhere but haven't found a satisfactory answer for this.

I have a business rule that runs on a change when it reaches the implementation stage. I need to get the start and end time for the change and send it to another system via a rest request.

I can get the details but I want to make sure the time I send is the epoch time corresponding to the US/Central timezone.

The code as of now is as follows:

var start_date = current.start_date.dateNumericValue()/1000;

var end_date = current.end_date.dateNumericValue()/1000;

var ps_dt = new GlideDateTime();

gs.addInfoMessage(start_date);

gs.addInfoMessage(end_date);

gs.addInfoMessage(ps_dt); //The current time

var tz = Packages.java.util.TimeZone.getTimeZone("US/Central");

//Set the timezone

ps_dt.setTZ(tz);

var offset = ps_dt.getTZOffset()/1000;

start_time=start_date + offset;

end_time= end_date + offset;

I was assuming that the start_date and end_date values would be in GMT/UTC but that doesn't seem to be the case. Those values are in US/Central time. That may be because the user time that created the change is US/CT.

I want to make sure the time that I send is always in US/CT regardless of the time zone in the system.

Regards,

Karan.

1 ACCEPTED SOLUTION

Hi Karanpreet,



Date/time fields are always stored in UTC/GMT format and displayed based on user's timezone in ServiceNow



Regards


Ankur


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

View solution in original post

5 REPLIES 5

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Karanpreet,



Since you are explicitly setting the timezone using the package call here is the exact value for US/Eastern



var tz = Packages.java.util.TimeZone.getTimeZone("Canada/Eastern");



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


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

Hi Ankur,



The tz setting is already done using the package call to the US/Central Time(Not Eastern Time) but the problem is the time I get from the start_date field itself is in US/CT not GMT and if this value varies according to the user creating the change, this won't work.



Regards,


Karan


Hi Karanpreet,



Date/time fields are always stored in UTC/GMT format and displayed based on user's timezone in ServiceNow



Regards


Ankur


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

Thanks Ankur! I got the issue solved, I had trouble doing some conversions.



Regards,


Karan