How To Check Differnce Betwen Two Dates Using Client Script

Abhijeet Pawar
Tera Contributor

Hello All,

I am new in ServiceNow Development can anyone please guide me on how to check the difference between the two dates?

my requirement is I want to find the difference between the date of creation of the incident and the date of updation of the incident.

Thank You.

1 ACCEPTED SOLUTION

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Abhijeet Pawar 

 

If you are looking for Date Difference when Incident is Updated, it is more recommended to use After Business Rule on Update with your required Condition. Use below code there:

 

var duration = gs.dateDiff(current.sys_created_on.getDisplayValue(), current.sys_updated_on.getDisplayValue(), false);

current.<duration_field> = duration;

 

Still if your situation as to use Client script, you can use below code:

 

Client Script:

 

var strt = g_form.getValue('sys_created_on');
  var end = g_form.getValue('sys_updated_on');
  var ajax = new GlideAjax('AjaxDurCalc');
  ajax.addParam('sysparm_name','durCalc');
  ajax.addParam('sysparm_strt',strt);
  ajax.addParam('sysparm_end',end);
ajax.getXML(DateDiff);
 
function DateDiff(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  g_form.setValue('<duration_field>', answer);
}

 

Script Include:

 

Name: AjaxDarCalc

Client Callable: TRUE/Checked

 

var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {

durCalc: function() {
  return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);

},


});

 

NOTE: The gs.dateDiff() method is not available in scoped applications.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

View solution in original post

7 REPLIES 7

AnubhavRitolia
Mega Sage
Mega Sage

Hi @Abhijeet Pawar 

 

If you are looking for Date Difference when Incident is Updated, it is more recommended to use After Business Rule on Update with your required Condition. Use below code there:

 

var duration = gs.dateDiff(current.sys_created_on.getDisplayValue(), current.sys_updated_on.getDisplayValue(), false);

current.<duration_field> = duration;

 

Still if your situation as to use Client script, you can use below code:

 

Client Script:

 

var strt = g_form.getValue('sys_created_on');
  var end = g_form.getValue('sys_updated_on');
  var ajax = new GlideAjax('AjaxDurCalc');
  ajax.addParam('sysparm_name','durCalc');
  ajax.addParam('sysparm_strt',strt);
  ajax.addParam('sysparm_end',end);
ajax.getXML(DateDiff);
 
function DateDiff(response) {
  var answer = response.responseXML.documentElement.getAttribute("answer");
  g_form.setValue('<duration_field>', answer);
}

 

Script Include:

 

Name: AjaxDarCalc

Client Callable: TRUE/Checked

 

var AjaxDurCalc = Class.create();
AjaxDurCalc.prototype = Object.extendsObject(AbstractAjaxProcessor, {

durCalc: function() {
  return gs.dateDiff(this.getParameter('sysparm_strt'),this.getParameter('sysparm_end'), false);

},


});

 

NOTE: The gs.dateDiff() method is not available in scoped applications.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023

Hello @AnubhavRitolia I am getting null output.

Hi @Abhijeet Pawar ,

 

please post a screen dump of your calculation together with which fields you are trying to calculate the difference between.

 

if my answer has helped with your question, please mark as accepted solution and give a thumb up.

 

Best regards

Anders

 

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/

Hi @Abhijeet Pawar 

 

Can you please share the code you have written.

 

Please mark this as correct answer and helpful if it resolved, or mark this helpful if this help you to reach towards solution.

Thanks
Anubhav Ritolia
ServiceNow Rising Star 2023