IBM BPM – Date Diff Code

Here’s a few blocks of code samples that I like to reference when trying to deal with Dates in IBM BPM.

This first block (Date Difference Calculation Testing) helps you determine an integer number of dates between two TWDate variables.

// Date Difference Calculation Testing
// Date1 + xNumberOfDays = Today
// xNumberOfDays = Today - Date1
 
tw.local.aDate = new TWDate(); //SETUP SAMPLE
tw.local.aDate.parse("05/15/2005","MM/dd/yyyy"); //SETUP SAMPLE
 
if (tw.local.aDate && tw.local.aDate < new TWDate()) {
    // Today...as in right now
    var tday = new Date();
    
    // convert to native Java Date
    var aDay = tw.local.aDate.toNativeDate(); 
    var one_day = 1000*60*60*24; // milliseconds in 1 day
    
    var tm = tday.getTime(); // milliseconds for date1
    var am = aDay.getTime(); // milliseconds for date2
    
    var diff = tm - am; // milliseconds difference
    
    tw.local.dateInt = diff; // this is actually a decimal
    
    // convert milliseconds to days
    var days = Math.round(Math.abs(diff/one_day)); 
    
    // Integer of number of days between aDate and today
    tw.local.dateIntTwo = days; 
}

This is a cool way to tell if a date variable is within a specific threshold.Ā  My use case was that the application stored when it made a specific integration call and then if we came back and needed that call again we’d check to see when it was last invoked.Ā  This could easily be done with caching or something fancier but for this scenario a simple “was the last call too long ago?” bit of logic was all that we needed.

// Assume the service needs to be called again
tw.local.refreshNeeded = true;
 
// aDate is the value of the last time the service was called
if (tw.local.aDate) {
    var lastCall = tw.local.aDate.toNativeDate();
    var now = new Date();
    // EPV to store threshold in seconds to consider time stamp stale
    var threshold = Number(tw.epv.refreshThreshold);
    var thresholdMS = threshold * 1000;
    var lcMS = lastCall.getTime();
    var nowMS = now.getTime();
    var diff = (nowMS - lcMS);
    if (diff <= thresholdMS) {
        tw.local.refreshNeeded = false;
    } 
}

One thought on “IBM BPM – Date Diff Code

  1. Tamal Das's avatar Tamal Das October 28, 2020 / 12:13 pm

    Its pretty helpful… šŸ™‚

    Like

Leave a comment