Sidebar

Why does qie.formatDate('YYYY-MM-dd', '2013-12-31') return the value '2014-12-31'?

0 votes
790 views
asked Mar 11, 2015 by mike-r-7535 (13,830 points)

Why does qie.formatDate() return the wrong year in this statement?

qie.info(qie.formatDate('YYYY-MM-dd', '2013-12-31'));

I found that only some years on December 29th, 30th, and 31st, are these years incorrect.

Here is my test mapping function I setup to isolate when the issue occurs by picking 10000 random dates, and only printing ones that didn't match:

function testFormatDate(date) {
   var formattedDate = qie.formatDate('YYYY-MM-dd', date);
   if (!StringUtils.equals(date, formattedDate)) {
      qie.warn(date + " was formatted into: " + formattedDate);
   }
}
function getRandomInt (min, max) {
   return Math.floor(Math.random() * (max - min + 1)) + min;
}
function getRandomDateString (minLong, maxLong) {
   var date = new java.util.Date(getRandomInt(minLong, maxLong));
   var sdf = new java.text.SimpleDateFormat('YYYY-MM-dd');
   return sdf.format(date);
}
function getDateLong(dateString) {
   return qie.deduceDate(dateString).getTime();
}
 
var minLong = getDateLong('2000-01-01');
var maxLong = getDateLong('2020-01-01');
 
for (var i = 0; i < 100000; i++) {
   testFormatDate(getRandomDateString(minLong, maxLong));
}

2 Answers

0 votes
 
Best answer
As of QIE version 2.0.44, using 'YYYY' is the same as using 'yyyy'.  If you need to format the date with the "week year", see https://www.qvera.com/kb/index.php/1202/
answered Mar 16, 2016 by ron-s-6919 (4,480 points)
0 votes

The answer is to use the correct DateFormat pattern.  Lowercase 'y' should be used instead of uppercase 'Y'.

http://stackoverflow.com/questions/8686331/y-returns-2012-while-y-returns-2011-in-simpledateformat

For the documentation on DateFormat patterns, please see the Java documentation:

 
answered Mar 11, 2015 by mike-r-7535 (13,830 points)
...