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

need to get difference between create and closed dates in days and hours.

anshul_jain25
Kilo Guru

hi

i want to create a new field where i want to display difference of create and close date once request gets closed.

and i want difference in days and hours.

how can i achieve this.

1 ACCEPTED SOLUTION

Shishir Srivast
Mega Sage

Please use the datediff function to calculate the difference between the date with boolean value as false.


dateDiff(String, String, boolean):


  • boolean bnumericValue - true to return difference in number of seconds as a string, false to return difference in the format ddd hh:mm:ss.


GlideSystem Date and Time Functions - ServiceNow Wiki


View solution in original post

6 REPLIES 6

Aditya Mallik
ServiceNow Employee
ServiceNow Employee

You can use the column "Resolve time" (calendar_stc) on sc_request table. That already captures the time taken for request to get resolved / closed.


anjalichoudhary
Kilo Guru

Hi Anshul,



You have to use Date and Time functions to achieve this.



You can refer below links as well:



Find difference between two dates


Using Date and Time Fields - ServiceNow Wiki



Client script:


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


  var strt = g_form.getValue('<start_field>');//set this as create date


  var end = g_form.getValue('<end_field>');//set this as close date


  var ajax = new GlideAjax('AjaxDurCalc');


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


  ajax.addParam('sysparm_strt',strt);


  ajax.addParam('sysparm_end',end);


  ajax.getXMLWait();


  var answer = ajax.getAnswer();


  g_form.setValue('<duration_field>', answer);


}



Script include


Name:AjaxDurCalc


Client callable:checked,client:checked


script:


var AjaxDurCalc = Class.create();


AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {


durCalc: function() {


  return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);


}


});



Please mark Correct/Helpful as per impact of the response!!



Thanks,


Anjali