using setDisplayValue to control date field value to match user's date format

patricklatella
Mega Sage

Hi all,

I've got a client script and script include that I'm using to set a date field based on the value of a variable on an asset record.   I need to incorporate some code to set the value in the new field to match the user's date format to ensure there are no errors.   Would using GlideDateTime and setDisplayValue be the way to go? my script below is setting the field correctly, but it's setting it in the system default format, I need it to set the value in the user's date format.   thanks!

right now I have this for client script:

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

      if (isLoading ) {

          return;

    }

if (newValue == ''){

g_form.clearValue('computer_retirement_date');

return;

}

var ga = new GlideAjax('u_Computer_Requests_Ajax');

      ga.addParam('sysparm_name', 'getRetirementDate');

ga.addParam('sysparm_ci', g_form.getValue('current_computer'));

      ga.getXML(dateLookup); // Always try to use asynchronous (getXML) calls rather than synchronous (getXMLWait)

}

// Callback function to process the response returned from the server

function dateLookup(response) {

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

g_form.setValue('computer_retirement_date',answer);

}

and this is my script include:

getRetirementDate: function(){

var retVal; // Return value

var ci     = this.getParameter('sysparm_ci');

var hardwareRec   = new GlideRecord('alm_hardware');

hardwareRec.addQuery('ci',ci);

hardwareRec.query();

if(hardwareRec.next())

{

retVal = hardwareRec.retirement_date;

}

return retVal;

1 ACCEPTED SOLUTION

modify the script include as:



getRetirementDate: function(){


var gdt = new GlideDateTime();


var retVal ; // Return value


var ci     = this.getParameter('sysparm_ci');


var hardwareRec   = new GlideRecord('alm_hardware');


hardwareRec.addQuery('ci',ci);


hardwareRec.query();


if(hardwareRec.next())


{


gdt= hardwareRec.retirement_date;


retVal = gdt.getDisplayValue();


}


return retVal;


View solution in original post

5 REPLIES 5

sethivarun
Kilo Guru

Hi Patirck,


You can define the format in set display value



3.15 setDisplayValue(value, format)

Sets a date and time value using the current user's time zone and the specified date and time format. This method throws a runtime exception if the date and time format used in the valueparameter does not match the format parameter. You can retrieve the error message by calling getErrorMsg() on the GlideDateTime object after the exception is caught. This method is available starting with the Eureka release.


3.15.1 Input Fields

Parameters:


  • value - (String) The date and time in the current user's time zone.
  • format - (String) The date and time format to use to parse the value parameter.

3.15.2 Output Fields

Returns: void


3.15.3 Example

var gdt = new GlideDateTime("2011-02-02 12:00:00"); gdt.setDisplayValue("20-5-2011 12:00:00", "dd-MM-yyyy HH:mm:ss"); gs.print(gdt.getValue()); 

Output:


2011-05-20 19:00:00

Hi Varun,


I'm seeing that, but am having trouble seeing how I add that in to my client script...can you guide me?


will the system pick up the user's specified date format field value?   or do I need an additional query to the user's record to get that?


modify the script include as:



getRetirementDate: function(){


var gdt = new GlideDateTime();


var retVal ; // Return value


var ci     = this.getParameter('sysparm_ci');


var hardwareRec   = new GlideRecord('alm_hardware');


hardwareRec.addQuery('ci',ci);


hardwareRec.query();


if(hardwareRec.next())


{


gdt= hardwareRec.retirement_date;


retVal = gdt.getDisplayValue();


}


return retVal;