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 pradeepksharma,


                                    This is not working in my Scoped App. Its bcoz of getXMLWait(),   I have tried   using getXML(), getXML(callback) as well as suggested in some pages. Any help on this???


Brand new to SNow, thanks for this script, it sets the duration field (u_duration) I created just fine, however, there is an unexpected issue that I can't figure out.


When the duration is set, it seems that the 'Name' of the change is altered.


In the change list, you see 'CHG000368' but when you open it, the tab header is the duration (i.e. '1 Day') and the filter on the Change Tasks tab says 'Change Request = 1 Day


Also the notifications that go out contain text like "Change Request '1 Day' Approval Request" and the task name is 'Click Here to Approve 1 Day'



Where so I start looking for why this is happening?


Pradeep,



Is there something I can add so it returns only the day?



Right now I am getting "4 00:00:00"....I only need the 4 since my fields are only date fields.   I don't need the time at all.



EDIT:   Disregard...I changed your script include gs.datediff to "true" which gave me seconds and then I divided that by 86,400 to get days.



Please mark this response as correct and/or helpful if it assisted you with your question.
Steven

Hello Pradeep,

Can you please help me with this thread which is similar to this thread. 

https://community.servicenow.com/community?id=community_question&sys_id=bf8b249cdb4f9b04e0e80b55ca9619c6

Hi Pradeep,

Will it be working even in scoped app for using 'GlobalAbstractAjaxProcessor' ?