How to calculate duration between two dates in business rule?

chenglo
Tera Contributor

Hi Experts,

I would to have your support for my issue. I'm creating a business rule to get data into another table for reports purpose. I want to calculate the duration between two dates that I have in my table. I'm currently using gs.dateDiff(); but seems the output isn't correct. Can any one please help me on this?

eg: opendate is "28-Aug-2018" // get from creation date

      supdate is "30-Aug-2018" // I get from supply team approval date

      Result = 14 // which is the number of days but in fact it doesn't correct. It's only 2 days, so the result I need to get 2.

 

(function executeRule(current, previous /*null when async*/) {
 
 // Add your code here
  
  if(current.u_vendor_aging_r_s =='Supply Completed'){
   
   // Query from sysapproval and insert to the table
   var recSysapproval = new GlideRecord('sysapproval_approver');
   recSysapproval.addQuery('sysapproval', current.sys_id);
   recSysapproval.addQuery('state', "Approved");
   recSysapproval.orderByDesc('sys_created_on');
   recSysapproval.setLimit(1);
   recSysapproval.query();
   
   while (recSysapproval.next()){
    var grvendor = new GlideRecord('u_vendor_master_data_aging_report');
    
    grvendor.initialize();
    
    grvendor.u_tsc_number = current.sys_id;
    grvendor.u_supply_appr_date = recSysapproval.sys_updated_on;
    grvendor.u_supply_approver = recSysapproval.approver.getDisplayValue();
    grvendor.u_company_code = current.variables.Company.getDisplayValue();
    grvendor.u_vendor_name = current.variables.vendor_name.getDisplayValue();
    grvendor.u_vendor_code = current.variables.vendor_code_by_TSC.getDisplayValue();
    grvendor.u_currency = current.variables.Currency.getDisplayValue();
    grvendor.u_type = current.variables.re_type.getDisplayValue();
    grvendor.u_request_date = current.variables.re_date.getDisplayValue();
    grvendor.insert();
    
    gs.addInfoMessage('The record has been inserted to Table');

     var opendate = current.u_request_date.getDisplayValue();
     var supdate = grvendor.u_supply_appr_date.getGlideObject();
     grvendor.u_aging_sup = gs.dateDiff(supdate.getDisplayValue(),opendate.getDisplayValue(),false);
     grvendor.update();
     gs.addInfoMessage('Effected the Aging');

    }
  
 })(current, previous);

 

Thanks for your support,

Cheng

1 ACCEPTED SOLUTION

HI,

I am confused here with this var opendate = current.u_request_date.getDisplayValue();

 

 from where u_request comes from.

Thanks,
Ashutosh

View solution in original post

11 REPLIES 11

Magnum1
Tera Expert

I was having the same issue. Here's the code I used to and it works in my "Business Rule).

    var sdate = new GlideDateTime(current.conf_begin_date);
    var edate = new GlideDateTime(current.conf_end_date);
    var diffSeconds = GlideDateTime.subtract(sdate, edate);
    var dur1 = new GlideDuration();
    dur1.setValue(diffSeconds.getValue());
    
    var numdays = parseInt(dur1.getDurationValue());  

 

From:Magnum

Thanks just what I was looking for to calculate dates. Resulted in this:

 

var daysLimit = 3;
var startDate = new GlideDateTime(current.start_date);
var cabDate = new GlideDateTime(current.cab_date);
startDate = startDate.getDate();
cabDate = cabDate.getDate();

var diffSeconds = GlideDateTime.subtract(startDate.getDate(), cabDate.getDate());
var dur1 = new GlideDuration();
dur1.setValue(diffSeconds.getValue());

var numDays = parseInt(dur1.getDurationValue());

if (daysLimit >= numDays){
	gs.addInfoMessage(numDays + ' day(s) between start and cab');
}