The Zurich release has arrived! Interested in new features and functionalities? Click here for more

getting datetime field in client script returns wrong time

Nate23
Mega Guru

Hello Developers,

I have run into an issue and have found a solution, but was wondering if there was something I was missing.... an easier way?

Issue: I have a reference variable on a catalog item to the user table. when someone selects a user I have an onchange script that gets that reference with a callback function to get a custom date/time field. The issue is that when I view it on my profile on the user record it displays the time in my timezone. when I pull it with this script it pulls it in the systems timezone. So it may be 4 hours off due to time differences.

client script:

function onChange(control, oldValue, newValue, isLoading) {

var caller = g_form.getReference('hiring_manager', doAlert); // doAlert is our callback function

}

function doAlert(caller) { //reference is passed into callback as first arguments

try{

//alert(caller.u_test.getDisplayValue());

var ajax = new GlideAjax('ClientDateTimeUtils');

ajax.addParam('sysparm_name', 'setTimeZone');

ajax.addParam('sysparm_dt', caller.u_name_of_date_field_on_user);

ajax.getXML(doSomething);

}catch(e){

alert(e);

}

}

function doSomething(response){

var answer = response.responseXML.documentElement.getAttribute("answer");

g_form.setValue('name of time variable',answer);

}

Current solution:

I have a glideajax call in my onchange client script to a script include that simply returns the date by :

setTimeZone: function(){

var date = this.getParameter('sysparm_dt');

var dt = new GlideDateTime(date);

return dt.getDisplayValue();            

}

this works but I cant help but feel there is an easier way?

If you have any definitive knowledge on this, I will be very grateful.

Thanks,

Nate

6 REPLIES 6

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Nathan,



When you use the date/time field in script it takes the time in UTC i.e. system always stores time in GMT.


But based on user timezone it displays on the form.


when we use getDisplayValue() it returns the time stored in system which is a GMT time.



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

yes true to all above, but my question is there an easier way than having to create a glideajax call... getdisplayvalue on a gliderecord is not supported via client script.



so my conclusion is still to get time in users timezone/format I have to do this ajax call. The questions remains the same" is there an easier way?"



Thanks,


Nate


Hi Nathan,



I don't think so. I never came across this requirement



Regards


Ankur


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

Ankur,



I advice you to not use bot 'getReference" and "GlideAjax' at the same time. This requires a call to the server.


I also advice you to use GlideAjax than getReference because first it is a best practice recommended from servicenow and getReference may decrease your instance's preference.



On your script above, if you choose to use a the GlideAjax Methode, then get rid of the getreference methode.


Change this line from


  1. ajax.addParam('sysparm_dt', caller.u_name_of_date_field_on_user);     to
  2. ajax.addParam('sysparm_dt', g_form.getValue('YOUR REFERENCED FILD NAME'));    


And within your script a little GlideRecord to get the u_name_of_date_field_on_user value.




If you choose to use gerReference method, It could be straighforward like following : GlideForm (g form) - ServiceNow Wiki


You can directly dot walk to get the info