Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Issue with converting Timezone

Chandresh
Tera Guru

Hello Folks,

I am trying to set a timezone in a script but it is not working fine. I am checking below code in the background scripts. No idea, what I am missing:

var gr=new GlideRecord("sn_customerservice_case_staging");

gr.addQuery('u_case','f4235c63db87b6003f7d388ffe961965');            

gr.query();

while(gr.next()){

var str= gr.u_time_stamp;                                                  

gs.print(str);

var dateArr = str.split(' ');

var date = dateArr[0];

var time= dateArr[1];

var datetime= date+' '+time;

gs.print(datetime);                                                                             //this is giving me date time in correct format and is in GMT and I am trying to convert this time to a different timezone.

var tz = gs.getSession().getTimeZone();     //this is giving correct timezone in which I want to convert my time

gs.print(tz);                                                                                               //output of session timezone is correct

var abc= datetime.setTZ(tz);

gs.print(abc);                                                                                     //output is undefined.

}

1 ACCEPTED SOLUTION

Community Alums
Not applicable

Hi Chandresh,




Please try by making the following changes :



var gr=new GlideRecord("sn_customerservice_case_staging");


gr.addQuery('u_case','f4235c63db87b6003f7d388ffe961965');          


gr.query();


while(gr.next()){


var str= gr.u_time_stamp;                                                


gs.print(str);



var dateArr = str.split(' ');


var date = dateArr[0];


var time= dateArr[1];


var datetime= date+' '+time;


gs.print(datetime);                                                                             //this is giving me date time in correct format and is in GMT and I am trying to convert this time to a different timezone.


var tz = gs.getSession().getTimeZone();     //this is giving correct timezone in which I want to convert my time


gs.print(tz);                                                                                               //output of session timezone is correct



var gDT = new GlideDateTime(datetime);


gDT.setTZ(tz);


var abc = gDT.getDisplayValue();



gs.print(abc);                                                                                     //output is undefined.


}


View solution in original post

4 REPLIES 4

Shishir Srivast
Mega Sage

Hi Chandresh,



I am not sure what is the purpose of these codes, when you are doing the same thing is datetime vaiable



var dateArr = str.split(' ');


var date = dateArr[0];


var time= dateArr[1];


var datetime= date+' '+time;




Can you just try this and check if it helps.



var gr=new GlideRecord("sn_customerservice_case_staging");


gr.addQuery('u_case','f4235c63db87b6003f7d388ffe961965');          


gr.query();


while(gr.next()){


var str= gr.u_time_stamp;    


//var gdt = new GlideDateTime(str);                                            


var tz = gs.getSession().getTimeZone();                                                                                                


gs.print(str.setTZ(tz));                                                                                    


}



setTZ(tz) : http://wiki.servicenow.com/index.php?title=GlideDateTime#setTZ.28tz.29


Sets the time zone of the GlideDateTime object to be the specified time zone.



Also, just to trouble the issue please check if it works.


var tz = gs.getSession().getTimeZone();


var gdt = new GlideDateTime();


gdt.setTZ(tz)


Community Alums
Not applicable

Hi Chandresh,




Please try by making the following changes :



var gr=new GlideRecord("sn_customerservice_case_staging");


gr.addQuery('u_case','f4235c63db87b6003f7d388ffe961965');          


gr.query();


while(gr.next()){


var str= gr.u_time_stamp;                                                


gs.print(str);



var dateArr = str.split(' ');


var date = dateArr[0];


var time= dateArr[1];


var datetime= date+' '+time;


gs.print(datetime);                                                                             //this is giving me date time in correct format and is in GMT and I am trying to convert this time to a different timezone.


var tz = gs.getSession().getTimeZone();     //this is giving correct timezone in which I want to convert my time


gs.print(tz);                                                                                               //output of session timezone is correct



var gDT = new GlideDateTime(datetime);


gDT.setTZ(tz);


var abc = gDT.getDisplayValue();



gs.print(abc);                                                                                     //output is undefined.


}


Thank you guys, this worked.


Hi Zeeshan,



One more query, when i am using this converted date field in web services it is showing data in GMT only. Is there any way we can change the value as well.


Right now, I believe this script is changing the display value.