Convert the date time field from user time zone to eastern time zone in business rule.

vinothkumar
Tera Guru

I am using the below code in business rule to get the change start date and split the date and time values and insert into custom table.

While inserting the record in custom table, the value has to be in eastern time zone. Since the date and time are string field in custom table, I am facing difficulties when the user is in different time zone.

 

Example: If the user is in Central time zone, the display value of the date is copied into target field. However before copying it has to be converted to Eastern time zone irrespective of user time zone. 

 

Can anyone help me on this?

 

if(current.start_date.getDisplayValue()!=""){
var date_time = current.start_date.getDisplayValue();

var date_split = date_time.split(" ")[0];
var time_split = date_time.split(" ")[1];

var month = date_split.split("-")[0];
var day = date_split.split("-")[1];
var year = date_split.split("-")[2];

var hours = time_split.split(":")[0];
var minutes = time_split.split(":")[1];
var seconds = time_split.split(":")[2];

newdate = date_split;
newtime = hours + ":" + minutes;
}

target.u_change_start_date = newdate;
target.u_start_time = newtime;

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@vinothkumar 

refer sample script to get the time in Eastern timezone

Remember this would work only in global scope and not custom scope as it uses Packages call

var time = new GlideDateTime();

gs.info('GMT time is->'+time);

var tz = Packages.java.util.TimeZone.getTimeZone('US/Eastern');
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
gs.info('Eastern time is->'+time);

Output:

find_real_file.png

Regards
Ankur

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

View solution in original post

9 REPLIES 9

vinothkumar
Tera Guru

Can anyone help me on this.

There are some simple OOB functions available to change the timezone. Please go to the below link.

https://community.servicenow.com/community?id=community_article&sys_id=8704fc76db468194bb4a474d13961...

Regards,

Nayan

Ankur Bawiskar
Tera Patron
Tera Patron

@vinothkumar 

refer sample script to get the time in Eastern timezone

Remember this would work only in global scope and not custom scope as it uses Packages call

var time = new GlideDateTime();

gs.info('GMT time is->'+time);

var tz = Packages.java.util.TimeZone.getTimeZone('US/Eastern');
time.setTZ(tz);
var timeZoneOffSet = time.getTZOffset();
time.setNumericValue(time.getNumericValue() + timeZoneOffSet);
gs.info('Eastern time is->'+time);

Output:

find_real_file.png

Regards
Ankur

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

@vinothkumar 

Hope you are doing good.

Let me know if I have answered your question.

If so, please mark appropriate response as correct & helpful so that this thread can be closed and others can be benefited by this.

Regards
Ankur

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