Freemarker provides powerful formatting options for working with dates, whether you’re displaying timestamps, converting numeric values to date formats, or transforming how a date is shown.
Formatting Date Values
When working with m-Power, it’s recommended to first convert your value to a date using ?date(...)
with the correct incoming format. Once the value is treated as a date, you can then format it using ?string(...)
.
Example
If your field is stored as a string in yyyyMMdd
format (e.g., "20030408"
), use the following:
${row.ORDERDATE?date("yyyyMMdd")?string["MM/dd/yyyy"]}
Sample Output:
04/08/2003
SimpleDateFormat Pattern Reference Table
Freemarker allows you to use Java’s SimpleDateFormat
patterns with the ?string
built-in. Below are some common examples:
Pattern | Meaning | Example Output |
---|---|---|
y | Year (e.g., yy = 2-digit, yyyy = 4-digit) | 03 , 2003 |
M | Month (1–12) | 4 |
MM | Two-digit month (01–12) | 04 |
MMM | Abbreviated month name | Apr |
MMMM | Full month name | April |
d | Day of the month (1–31) | 8 |
dd | Two-digit day of the month | 08 |
E | Abbreviated day of the week | Tue |
EEEE | Full day of the week | Tuesday |
h | Hour (1–12, no leading zero) | 9 |
hh | Hour (1–12, two digits) | 09 |
H | Hour (0–23, no leading zero) | 21 |
HH | Hour (0–23, two digits) | 21 |
m | Minutes (0–59) | 4 |
mm | Two-digit minutes | 04 |
s | Seconds (0–59) | 9 |
ss | Two-digit seconds | 09 |
a | AM/PM marker | PM |
z | Time zone abbreviation | PDT |
Example Usage in Freemarker
${row.ORDERDATE?date("yyyy-MM-dd")?string["MM/dd/yyyy"]}
Output:
04/08/2003
Other Examples in Freemarker
${row.ORDERDATE?date("yyyyMMdd")?string["dd.MM.yyyy, HH:mm"]}
${row.ORDERDATE?date("yyyyMMdd")?string["EEEE, MMMM dd, yyyy, hh:mm a '('zzz')'"]}
${row.ORDERDATE?date("yyyyMMdd")?string["EEE, MMM d, ''yy"]}
Sample Output:
08.04.2003, 21:24
Tuesday, April 08, 2003, 09:24 PM (PDT)
Tue, Apr 8, '03
Formatting Non-Date Values into Dates
Sometimes, your data might not be a date value but still represent a date component—like a numeric month (e.g., 1, 2, 10). You can still use Freemarker to display these values as formatted dates.
Example: Numeric Month → Month Abbreviation
${row.MONTHNUM?date('M')?string('MMM')}
Sample Output:
Jan, Feb, Oct, Nov
Example: Numeric Month → Full Month Name
${row.MONTHNUM?date('M')?string('MMMM')}
Sample Output:
January, February, October, November
Notes
These formats can be customized to meet your localization or styling needs using the SimpleDateFormat
symbols. Refer to Java’s SimpleDateFormat documentation for the full list of formatting options.