Home | Dynamic Content | Advanced FreeMarker: How to Change the Date Format in an Email

Advanced FreeMarker: How to Change the Date Format in an Email

Halloween is near, but there are both tricks and treats to be found here! You know your FreeMarker basics, but what if I told you that was just the beginning? In this post, we are going to look at some advanced FreeMarker used to change date formats.

If you haven’t mastered your FreeMarker basics, not to worry! I recommend checking out this guest blog post – it will help you learn FreeMarker syntax.

FreeMarker will pull the date value from CRM in its raw format from the database. Often this is the US date format. With a few lines of FreeMarker, we can display this date in a number of ways including EU format.

The first thing we will do is assign a FreeMarker variable to the date field we want to work with. In my case, the schema name of my date field is new_date and I am assigning it the variable of mydate:

<#assign mydate=Recipient.contact.new_date />

Now that we have assigned the variable, we need to tell FreeMarker that we are working with a date field. This may seem redundant, but it will grant us access to built-ins or functions that are specific to date type variables. We are also going to specify its current format:

US Example: ${mydate?date(“MM/dd/yyyy”)}

EU Example: ${mydate?date(“dd/MM/yyyy”)}

Note: It is important that you know what the default date format FreeMarker is pulling. For example, October 2, 2017 is displayed as 10/02/2017 in the US but in the EU it is 02/10/2017. If we accidently assign the wrong date format above, the end result will not be as expected. I recommend running a quick test and print out the date field as is to see how it looks.

Building further on the above, we can now manipulate the components to display the data in a different way by converting it to a string and telling it which components to display. If our database displays dates in US format, but we have an EU audience, we can use the following code to switch the format:

${mydate?date(“MM/dd/yyyy”)?string(“dd/MM/yyyy”)}

This will display 10/02/2017 (US format) as 02/10/2017 (EU format).

There are many handy built-ins for dates converted to strings. Here are some other examples. These all assume the original date format is US, using the 10/02/2017 date:

${mydate?date(“MM/dd/yyyy”)?string(“dd MMMM yyyy”)} – Displays: 02 October 2017

${mydate?date(“MM/dd/yyyy”)?string.medium} – Displays: Oct 2, 2017

Here are some code snippets to show you how each of these render:

Email Template Code

Received Email

If you want more information on date built-ins, I highly recommend referencing this website.

Happy Coding!

2017-10-11T16:10:52+00:00 By |Dynamic Content, Email Marketing|0 Comments

About the Author:

mm
Emma Butler is a ClickDimensions Marketing Success Manager.

Leave A Comment