On change client script to calculate the difference in start date and end date and display the result in another field no_of_days in days

test1231998
Tera Contributor

I have two fields start date and end date for my table.I wanted to write an onchange client script such that upon selection of both dates the difference in dates should be auto populated in no-of_days field which is of type integer.

For eg:If start date is 09-09-2020 and end date is 11-09-2020 The no_of_days field should have 3.

15 REPLIES 15

madicherlas
Tera Contributor
function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var sdate = g_form.getValue('request_date');

   var reqdate = new Date(sdate);
   var today = new Date();
 // set Hours to zero
   reqdate.setHours(0, 0, 0, 0);
   today.setHours(0, 0, 0, 0);

 // calculate time differnce
   var difftime = reqdate.getTime()- today.getTime();
   var diffdays =  Math.round(difftime / (1000 * 60 * 60 * 24));


 //set Priorty values
   var priority = '';

   if(diffdays <= 2){
    priority = 'high';
   }else if (diffdays >= 3 && diffdays <= 10){
    priority = 'normal';
   }else if (diffdays >= 11){
    priority = 'low';
   }
   g_form.setValue('priority', priority);

   g_form.addInfoMessage('Priority set to' +' '+ priority + ' ' +'based on request date');
}