Moving from other reporting packages to BIRT, you will always find that one feature you had in your previous reporting tool that you can’t find right away in BIRT. As I’ve continued to adopt more of my company’s internal reports to BIRT, I found date formatting is something that can be a little frustrating. Our previous reporting tool could easily convert a Date/Time field to a short date of xx/xx/xx. For example, today’s date would be formatted as:
2012-05-03 15:35:17.342 = 5/3/12
This is the standard format we use for date fields in our company’s reports. However, BIRT doesn’t recognize years as a 2 digit number, it sees years as a 4 digit number. So when showing date/time fields in BIRT, as a short date, BIRT would default the conversion to:
2012-05-03 15:35:17.342 = 5/3/2012
So how do you get BIRT to show the short date format we’re used to? By concatenating parts of the date field and converting the year value to a two character string. The following code is entered as a Dynamic Text field in lieu of a Data field:
BirtDateTime.month(row["datetimefield"]) + "/" + BirtDateTime.day(row["datetimefield"]) + "/" + BirtStr.right((BirtDateTime.year(row["datetimefield"])).toString(), 2)
So what happens is the following:
BirtDateTime.month(row["datetimefield"])will give the numeric value of month.
+ '/'will add the '/' separator to the text string. The '+' ties the two text fields together so they string together.
BirtDateTime.day(row["datetimefield"])will give the numeric value of the day.
+ '/'will be the other date separator.
BirtStr.right((BirtDateTime.year(row["datetimefield"])).toString(), 2)does three steps in one statement:
- The inner function
BirtDateTime.year(row["datetimefield"])works like the previous functions and returns a 4 digit year value.
- The string modifier
.toString()converts the 4 digit Date value to a 4 character string.
- The outside Birt function
BirtStr.right( ), 2)trims the 4 character year string to just the right 2 characters.
- The inner function
So now we can format any Date/Time field to a short date format of 5/3/12.