The code field of Custom Emails and Smart Documents is rendered into HTML (and then PDF) using the Velocity Templating Language, which gives you access to basic programming structures, such as variables, logic, and loops. The full Velocity user guide is at: http://velocity.apache.org/engine/1.7/user-guide.html
Using the power of the Velocity Templating Language, we can change how dates are formatted in these emails & documents.
By default, dates from Infoplus appear in custom emails and smart documents like the following:
2019-10-30T00:00:00.000Z
How to change the format of this date is dependent on if the date is a standard date field from Infoplus or a custom date field.
Formatting a Standard Date Field
The value passed in by a standard date field is a date object, so we have access to methods like getMonth() and getYear(). An example of a standard date field is orderDate from the Order table. The following example changes the format of the date to MM/DD/YYYY using these methods:
#set($orderDateMonth = $record.orderDate.getMonth() + 1)
#set($orderDateYear = $record.orderDate.getYear() + 1900) #set($orderDateDate = record.orderDate.getDate())
#set($orderDate = "${orderDateMonth}/${orderDateDate}/${orderDateYear}")
This code set a variable called $orderDate. To display this formatted date in the document, just add it somewhere in the HTML of the document. For example:
<span style="color: #000000;">$!orderDate</span>
Formatting a Custom Date Field
The value passed in by a date custom field is a string object, rather than a date object, so the getMonth() and getYear() methods used in the previous example will not work.
In this example, we have a custom field called expectedReceiptDate. To change the format of the date to MM/DD/YYYY, we can use substring to pull out the information needed from the string. The following velocity code will make this happen:
#set($shortDate = $record.customFields.expectedReceiptDate.substring(5,7) + '/' + $record.customFields.expectedReceiptDate.substring(8,10) + '/' + $record.customFields.expectedReceiptDate.substring(0,4))
This code set a variable called $shortDate. To display this formatted date in the document, just add it somewhere in the HTML of the document. For example:
<span style="color: #000000;">$!shortDate</span>