The Zurich release has arrived! Interested in new features and functionalities? Click here for more

calculate difference between date

mregragui_ext
Tera Contributor

hello community,

i want to calculate the difference between start date and end date and populate the result on duration field.

i have create two scripts:

script include:

var lmsAjax = Class.create();
lmsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
getDateDiff: function() {
var d1 = new GlideDateTime();
d1.setDisplayValue(this.getParameter('sysparm_start'));
var d2 = new GlideDateTime();
d2.setDisplayValue(this.getParameter('sysparm_end'));
//var duration = new GlideDate().subtract(d1, d2);
var duration = new GlideDuration(d1.subtract(d2));
return duration.getNumericValue();
},
type: 'lmsAjax'
});

 

and client script:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
var start = g_form.getValue('start_date');
// var end = g_form.getValue('end_date');
   //Type appropriate comment here, and begin script below
var ga = new GlideAjax('lmsAjax');
ga.addParam('sysparm_name', 'getDateDiff');
ga.addParam('sysparm_start', start);
ga.addParam('sysparm_end', newValue);
ga.getXML(getDuration);
function getDuration(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
g_form.addInfoMessage('My Variable Value: ' + answer);
var days = (answer / (1000 * 60 * 60 * 24));
g_form.addInfoMessage('My Variable Value: ' + days);
var days1 = days + 1;
g_form.setValue('duration', days1);
}
 
}
 
and i am getting zero everytime in my duration field
3 REPLIES 3

Vaibhav_Nikam
Tera Guru

Hello @mregragui_ext 
you just update your script include function with the below script :

 

 
getDateDiff: function() {
var startdate =this.getParameter('sysparm_start');
var enddate = this.getParameter('sysparm_end');
var duration =GlideDateTime(enddate).getNumericValue() - GlideDateTime(startdate).getNumericValue();
return duration;
},
 
 
 In the client script, don't need to add the below line :
var days1 = days + 1;
 
 
PFA for result on my PDI.

If my response finds helpful, please indicate its helpfulness by selecting Accept as Solution and Helpful.

Thanks,

Vaibhav Nikam

Sumanth16
Kilo Patron

Hi @mregragui_ext ,

 

You can use the below code to calculate the duration between two dates.


getTimeDiff();  

function getTimeDiff(){  

var startDate = current.u_start_date.getGlideObject();

 var endDate = current.u_end_date.getGlideObject(); 

 current.u_duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);  

  }  

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

Maddysunil
Kilo Sage

@mregragui_ext 

I think Instead of returning the numeric value of the duration directly, it should return a string representation of the GlideDuration object.

Script Include:

 

var lmsAjax = Class.create();
lmsAjax.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
  getDateDiff: function() {
    var d1 = new GlideDateTime();
    d1.setDisplayValue(this.getParameter('sysparm_start'));
    var d2 = new GlideDateTime();
    d2.setDisplayValue(this.getParameter('sysparm_end'));
    
    // Calculate the difference between the dates
    var duration = new GlideDuration(d1.subtract(d2));
    
    // Return the duration as a string representation
    return duration.getDisplayValue();
  },
  type: 'lmsAjax'
});

 

Additionally, in the client script, you're passing the newValue of the 'end_date' field to the GlideAjax call, which seems incorrect. It should be the value of 'start_date' that needs to be passed as 'sysparm_start'

Client Script:

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
  if (isLoading || newValue === '') {
    return;
  }

  var start = g_form.getValue('start_date');
  var end = g_form.getValue('end_date');

  var ga = new GlideAjax('lmsAjax');
  ga.addParam('sysparm_name', 'getDateDiff');
  ga.addParam('sysparm_start', start); // Pass the start date as parameter
  ga.addParam('sysparm_end', end);     // Pass the end date as parameter
  ga.getXML(getDuration);
  
  function getDuration(response){
    var answer = response.responseXML.documentElement.getAttribute("answer");
    g_form.setValue('duration', answer); // Set the duration directly
  }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks