In a previous blog post, we discussed how to use FreeMarker to change the date format in an email. Once you have used FreeMarker to pull in that Date and Time field, depending on the field’s settings in CRM, it might end up in UTC format (2017-10-26T16:10:00Z). This is not exactly easy on the eyes, but we can use some FreeMarker tricks to separate the date and time, and even display them in an easier to read fashion. If you haven’t seen our previous blog post on changing date formats, I would encourage you to give it a quick read before attempting these more advanced functions.

As we previously discovered, FreeMarker will pull the date value from CRM in its raw format from the database. If the behavior of that Date and Time field has been set to User Local, UTC format will be shown. You can always double check your field’s settings by clicking on Form:

This will open the form editor. Next, double-click the Date and Time field you want to inspect:

Once this opens, click on the Details tab and click Edit:

This will display all the data regarding this field. At the very bottom, you will be able to see the behavior of the field including a footnote that talks about the field’s value:

So now we know that this field will be in a specific date format each time, we can break apart the date into sections called substrings and start to reformat things.

Let’s start with the date. We know that the date section of the result is always going to be the exact same: yyyy-mm-dd. By using the substring built in, we can instruct FreeMarker to separate this date out from the rest of the field. When building a substring, we have to tell FreeMarker where to start and where to end. If we count all the characters in this string, there are ten total. In programming, we start counting at zero. In our case, the first character of the date 2017-10-26T16:10:00Z is at index point 0. We want to include everything up to the T which is at index point 10. To denote this, we use a build-in called substring and it will look like this:


So, if our date was 2017-10-26T16:10:00Z our substring would now look like 2017-10-26. This is a great start but we don’t usually display a date with the year first. Now that we have pulled out the date, we can tell FreeMarker this substring is a date and reformat to make it easier to read:


If you recall from our previous blog post, once we define the current date format for FreeMarker, we can then rearrange the different components to display the date in various ways. This time I am going to show you how to include the day alongside the date:

${,10)?date(“yyyy-MM-dd”)?string(“EEEE dd MMMM yyyy”)}

This will change our substring of 2017-10-26 to Thursday 26 October 2017. This can be especially helpful when sending out event or appointment reminders.

Now that we have shown you how to split off the date, how about splitting out the time too? Well, we’re going to do the exact same thing with the substring method, except this time we’re not starting at 0, we want to choose everything from the T to just before the Z. The first number in our time substring 2017-10-26T16:10:00Z is at index point 11. We want everything before the Z which is at index point 19. So, our substring will look like this:


This will now pull as 16:10:00. While this might be enough, we can tell FreeMarker that this is a time and reformat in something easier for our readers’ eyes. Firstly, we need to tell FreeMarker we’re working with a time and what its current format is:


Now we have told FreeMarker that we are looking at hours, minutes and seconds. The capital HH here is significant as it is signifying to FreeMarker that we are working with the 24-hour clock. While this might be okay for European audiences, individuals in the US might prefer a 12-hour format with an AM/PM notation.

${,19)?time(“HH:mm:ss”)?string(“hh:mm a”)}

This will now change from 16:00:00 to 04:00 PM. Note how changing from HH to hh has converted the time to 12 hour. The addition of the a at the end also added an AM/PM component. Much easier to read! If we wanted to keep it at 24 hour we would revert to HH and drop the a.

Here’s an example of our results:

Happy Coding!