Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

Date Time Calculation

littlelingling0
Tera Contributor

Dear Sir,

I had create two fields in incident, one is 'assign date', another is 'resolved date'.

How can I calculate the date difference between 'assign date' and 'resolved date' by using client script? Then put the result into 'date diff' field.

Screen Shot 2017-05-17 at 16.10.10.png

Thanks.

Best regards,

Ling

1 ACCEPTED SOLUTION

drbz
Mega Expert

Hello,


you can use this example from wiki:


http://wiki.servicenow.com/index.php?title=Setting_the_Duration_Field_Value#Calculating_a_duration_a...



Example usage (incident form):


find_real_file.png


Client script:



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


    if (isLoading || newValue === '') {


          return;


    }



  var strt = g_form.getValue('sys_created_on');


  var end = g_form.getValue('opened_at');


 


  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.addInfoMessage(answer);


  g_form.setValue('u_dur', answer);


   


}



Script include (client callable):



var AjaxDurCalc = Class.create();


AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {


durCalc: function() {


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


  }


});


View solution in original post

5 REPLIES 5

Julian Hoch
ServiceNow Employee
ServiceNow Employee

There's two ways to perform date difference calculation on the client that I found the easiest.


One is to use the "getDateDiff" method of the "AjaxProjectTaskUtil" Script Include via Ajax, or to use getDateFromFormat(date, g_user_date_format) to convert the date to a format you can make calculations on the client.


drbz
Mega Expert

Hello,


you can use this example from wiki:


http://wiki.servicenow.com/index.php?title=Setting_the_Duration_Field_Value#Calculating_a_duration_a...



Example usage (incident form):


find_real_file.png


Client script:



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


    if (isLoading || newValue === '') {


          return;


    }



  var strt = g_form.getValue('sys_created_on');


  var end = g_form.getValue('opened_at');


 


  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.addInfoMessage(answer);


  g_form.setValue('u_dur', answer);


   


}



Script include (client callable):



var AjaxDurCalc = Class.create();


AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {


durCalc: function() {


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


  }


});


May I know how can I deduct the time which is non office hours? Just like the calculation for business duration..



thanks.