Sidebar

If I want to change the end of a date string if its zero's is this the most efficient way?, it feels not

0 votes
352 views
asked Oct 30, 2015 by (600 points)
edited Nov 2, 2015 by
var sSub;
var dPV145 = source.getNode("PV1-45.1");
 
// Get Length of PV1-45
var iLen=StringUtils.length(dPV145);
var iTestFor='000000';
var iReplace='235959';
 
// End test
var bEnds=StringUtils.endsWith(dPV145, iTestFor);
 
qie.debug('PV1-45 is ' + dPV145);
qie.debug('EndsWith is ' + bEnds);
 
// If date ends "0000" make it 235959
if (bEnds) {
   // Now make new PV145 all of PV145 except last 4 chars and add on our replace
   sSub=StringUtils.substring(dPV145, 0 , -6);
   qie.debug('Sub is ' + sSub);
   
   // New date string (Old first characters plus replacement)
   dPV145=sSub + iReplace;
   message.setNode('PV1-45', dPV145);
   
   qie.info('PV1-45 ended "' +iTestFor + '" so made "' + iReplace + '"');
}
 

 

1 Answer

0 votes
While the above code works the below code shortens the lines of code by utilizing JavaScript "Compound Statements" Using compound statements can sometimes help with readability but can also make the code harder to interpret. User preference can be employed here.
The two examples below perform the date manipulation using two different JavaScript methods using date and string functions.
 
Example Using JavaScript Date Functions:
 
var PV145 = source.getNode("PV1-45");
if (StringUtils.endsWith(PV145, '000000')) {
   var newDate = org.apache.commons.lang.time.DateUtils.addSeconds(qie.deduceDate(PV145), -1);
   message.setNode('PV1-45', qie.formatDate('yyyyMMddHHmmss', newDate));
}
 
Example Using JavaScript String Functions:
 
var PV145 = source.getNode("PV1-45");
if (StringUtils.endsWith(PV145, '000000')) {
   message.setNode('PV1-45', StringUtils.substring(PV145, 0 , -6) + '235959');
}
 
answered Dec 2, 2015 by gary-t-8719 (14,820 points)
...