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.

Find difference between two dates

arobertson
Tera Guru

Hi All,

 

I have two seperate dates in the format of DD-MMM-YYYY that i need to calculate the difference off. I need the answer to be in days. What's the best way to achieve this as i'm using a client script i cannot use anything gs.

1 ACCEPTED SOLUTION

Hi Alex,



Try this


Client script:


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


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


  var end = g_form.getValue('<end_field>');//set this as repair 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);


}


});


View solution in original post

16 REPLIES 16

you rocked it man thank you so much..........

Kamil Surtel
Mega Guru

Hi Alex, 

just a side note, while the solutions provided in the answers are valid, please be aware that there are some OOTB tools that could be helpful. For example, the Incident form has a field called Business duration that automatically calculates the time between creating the incident and its resolution.