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

Hi solutioner now,



I did look into that, but need to get the data from the following.



I need to take one date from "purchase_date", another date from "u_repair_date" and then display the difference in "u_date_difference"


Hi Alex,



Here is the updated script


.


You can use it as follows.


// Given two date/times as strings


var dateString1 = current.purchase_date;


var dateString2 = current.u_repair_date;



// Determine the difference as number of seconds (returns a string)


var diffSeconds = gs.dateDiff(dateString1, dateString2, true);



current.u_date_difference = diffSeconds;



Please mark this answer as Correct/Helpfull if it was really helpful for you.



Regards,


Solutioner


Logo.png


Enhance Knowledge NOW@ www.solutioningnow.com


http://www.solutioningnow.com


Thanks,



Problem is, i cannot use current. or gs. in a client script.


You can use newValue, oldValue, or make a thing like this :



var inc = new GlideRecord('u_your_table');


inc.addQuery('u_your_field');


inc.query();


while (inc.next())


      jslog('Data   = " + inc.your_field);



or use g_form like this :



var myValue = g_form.getValue('your_field');


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);


}


});