IBM App Connect (Cast Iron) function to add/subtract minutes

Posted by jack on October 21, 2016

One common requirement in integration is to set threshold dates based on a configuration setting for an offset from an existing dateTime value – either the current dateTime or one read in data. Unlike other languages and tools, neither App Connect / Cast Iron nor JavaScript have convenient functions to for example subtract 10 minutes from a given dateTime. I am sure many JavaScript developers have their own functions to do this, but App Connect only supports a sub-set of the JavaScript language (IBM are a bit coy about this!) but on the other hand it does provide date format functions we can take advantage of to reduce the amount of code lines required.

Here then is my custom JavaScript function code to create such a function – this one works in minutes, but you could extend to seconds or even milliseconds if you require….

I call this function addMinutesToDateTime_yyyyDDDHHmm

App Connect developers (&others) will recognise the suffix as a format string for date time entries. I chose that format because it converts to/from a string with a fixed length, and avoids handling months – DDD in App connect means “The day number in the year expressed as 3 digits, with 001 being 1st Jan” . NOTE – HH is the 24 hour clock – do not use hh wich is 12 hour clock.

The inputs to the function are an integer (positive or negative) ¬†that I named ¬†minsToAdd_mayBeNegative, and a date time represented as a string called¬†inputDateTime_yyyyDDDHHmm – as the name suggests, to convert an actual dateTime for input to the function use a standard Formal Date String function with the format entry….. well I think you know by now.

The output of the addMinutesToDateTime_yyyyDDDHHmm function then is passed to the Read Date String function – yes, with the same format string.

Here is the code within my custom function. Note I am not bothered about leap years, so possibly if you use over a period of more than 366 days you will be a day out…..if anyone wants to spend time elaborating to handle leap years, feel free.

var returnValue;
var outputYear =Number( inputDateTime_yyyyDDDHHmm.substr(0,4));
var outputDays =Number( inputDateTime_yyyyDDDHHmm.substr(4,3));
var outputHrs =Number( inputDateTime_yyyyDDDHHmm.substr(7,2));
var inputMins =Number( inputDateTime_yyyyDDDHHmm.substr(9,2));
var outputMins = inputMins + minsToAdd_mayBeNegative;
while(outputMins < 0){
outputMins =outputMins + 60;
outputHrs = outputHrs -1;
}
while(outputMins >=60){
outputMins =outputMins – 60;
outputHrs = outputHrs +1;
}
while(outputHrs<0){
outputHrs =outputHrs + 24;
outputDays = outputDays -1;
}
while(outputHrs>=24){
outputHrs =outputHrs – 24;
outputDays = outputDays +1;
}
while(outputDays<=0){
outputDays =outputDays +365;
outputYear = outputYear -1;
}
while(outputDays>366){
outputDays =outputDays -365;
outputYear = outputYear +1;
}
var outputDaysString=outputDays.toString();
while(outputDaysString.length <3){
outputDaysString = ‘0’ + outputDaysString;
}
var outputHrsString=outputHrs.toString();
while(outputHrsString.length <2){
outputHrsString = ‘0’ + outputHrsString;
}
var outputMinsString=outputMins.toString();
while(outputMinsString.length <2){
outputMinsString = ‘0’ + outputMinsString;
}

returnValue = outputYear + outputDaysString + +outputHrsString + outputMinsString;

return returnValue;

Categories: Cast Iron

Comments are closed.