Email script to display long date from date field

Joni_Freeman1
Level 1

Email script to display long date from date field

Hi everyone,

Thought I'd share this to help future teams in easily displaying long dates from date fields in Marketo. This script will display the weekday and full date in an email if pulling from a date field. We happen to be using a lead-type field here.

$date.format('EEEE dd MMMM, yyyy', ${convert.parseDate(${lead.YOUR FIELD HERE}, 'yyyy-MM-dd')})

This will display a date such as: "Monday 1 January, 2017".

Enjoy!

Joni

P.S. I'm not a coding expert so unfortunately won't be able to answer complex questions, sorry! Found how to insert the weekday from this post: A Few More Email Scripting Examples - Thanks to my colleague John Lau​ for initial work on the script!

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: Email script to display long date from date field

You forgot time zones, which will create nasty unexpected problems regarding the midnight hour.

Velocity (i.e. Java) Date and Calendar objects are tz-aware, and your pod tz (not instance tz) needs to be taken into account when instantiating Dates.

Joni_Freeman1
Level 1

Re: Email script to display long date from date field

A fair point! We are displaying a static date (university term start) and our audience is Australian only so it's not a major concern for our context, but others would need to be aware of it. Do you have any resources to recommend?

SanfordWhiteman
Level 10 - Community Moderator

Re: Email script to display long date from date field

This is how it's done:

#set( $dateOptions = {

  "fieldValue" : $lead.YOUR_DATE_FIELD_HERE,

  "formats" : {

    "in" : "yyyy-MM-dd",

    "out" : "EEEE dd MMMM, yyyy"

  },

  "timezones" : {

    "in" : "Australia/Sydney",

    "out" : "Australia/Sydney"

  },

  "locale" : $date.getLocale()

} )

## ---- No need to change anything below this line! ----

#set( $dateOptions.timezones.in = $date.getTimeZone().getTimeZone($dateOptions.timezones.in) )

#set( $dateOptions.timezones.out = $date.getTimeZone().getTimeZone($dateOptions.timezones.out) )

#set( $inDate = $convert.parseDate($dateOptions.fieldValue, $dateOptions.formats.in, $dateOptions.locale, $dateOptions.timezones.in) )

${date.format($dateOptions.formats.out, $inDate, $dateOptions.locale, $dateOptions.timezones.out)}

Note that by convention, a date without an explicit time is implicitly midnight (00:00) on that day, in the input timezone. This doesn't mean the Date doesn't have a time, it just luckily is at midnight which can mask a lot of problems. But it can't prevent major confusion that occurs across timezones depending on what else you do with the date.

Lots more Velocity + Marketo examples on my blog: http://blog.teknkl.com/tag/velocity

P.S. Even when you're working with what seems like a datetime in local tz, you can still have problems because of the Marketo pod timezone (not the same as your instance tz, for example my primary instance is in US Eastern but the pod is US Central).