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

Pradeep Sharma
ServiceNow Employee
ServiceNow Employee

Hi Alex,



Please refer this wiki link section 3.1


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



I hope this helps



Thanks


Pradeep Sharma


Hi Pradeep.



As present both of these dates are read only string fields.



Purchase Date: DD-MMM-YYYY & Life End Date: DD-MMM-YYYY


There are no defined methods for calculating date fields on the client as far as I'm aware. The way I had to handle this previously was to use a Glide Ajax call to a server-side script include, which would handle the comparison and return a string value. It's a little tricky to set up if you've never done it, but the wiki gives a pretty good explanation of the concept:



GlideAjax - ServiceNow Wiki


solutioningnow
Giga Guru

Hi Alex,


Here is the Solution.


There is OOB fumction "dateDiff(String, String, boolean) "


You can use it as follows.


// Given two date/times as strings


var dateString1 = '2014-01-01 12:00:00';


var dateString2 = '2014-01-01 13:00:00';



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


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



// JavaScript will coerce diffSeconds from a string to a number


// since diffSeconds is being compared to a number


if (diffSeconds <= 0) {


      gs.print(dateString1 + ' is on or after ' + dateString2);


} else {


      gs.print(dateString1 + ' is before ' + dateString2);


}



You can also visit wiki page


GlideSystem Date and Time Functions - ServiceNow Wiki



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/