How to change the format of a date in Custom Emails and Smart Documents

How to change the format of a date in Custom Emails and Smart Documents

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:

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:

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:

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>