Calculate Duration between start and end date

snuser10
Kilo Expert

Hello - I've planned start date and planned end date in change module.   We want to calculate the duration between the two dates in a seperate field.   Thoughts on BR script? I tried below with condition that date fields aren't blank but it doesn't seem to work.

calculateDiff();

function calculateDiff(){

  var outage_start = g_form.getValue('start_date');

  var outage_end = g_form.getValue('end_date');

  var ajax = new GlideAjax('AjaxDurCalc');

  ajax.addParam('sysparm_name','durCalc');

  ajax.addParam('sysparm_strt',start_date);

  ajax.addParam('sysparm_end',end_date);

  ajax.getXMLWait();

  var answer = ajax.getAnswer();

  g_form.setValue('u_estimated_task_duration', answer);

}

1 ACCEPTED SOLUTION

sachin_namjoshi
Kilo Patron
Kilo Patron

You can use below code to calcuate duration betweeen two dates.



  1. getTimeDiff();  
  2.  
  3.   function getTimeDiff(){  
  4.   var startDate = current.u_start_date.getGlideObject();  
  5.   var endDate = current.u_end_date.getGlideObject();  
  6.  
  7.   current.u_duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);  
  8.  
  9.  
  10.   }  


Regards,


Sachin


View solution in original post

7 REPLIES 7

sachin_namjoshi
Kilo Patron
Kilo Patron

You can use below code to calcuate duration betweeen two dates.



  1. getTimeDiff();  
  2.  
  3.   function getTimeDiff(){  
  4.   var startDate = current.u_start_date.getGlideObject();  
  5.   var endDate = current.u_end_date.getGlideObject();  
  6.  
  7.   current.u_duration = gs.dateDiff(startDate.getDisplayValueInternal(),endDate.getDisplayValueInternal(),false);  
  8.  
  9.  
  10.   }  


Regards,


Sachin


Works perfectly with duration field type.


Hi sachin, sachin.namjoshi


I am new to service now !


I have two fieldsstart date and end date. if the end date is greater than 30 days it should give an alert.



i have written a client script which is working in maintain items Try it but it is not working in service portal.



how can i display an alert box if end date is more than 30 days of start date using glide ajax.



Thanks in advance


Hi sachin.namjoshi,

How to put the source code on the client script or where?

Thank you