SOLVED

How do I modify the date format for a token using Velocity Script?

Go to solution
Anonymous
Not applicable

Hello Community,

We'd like to be able to change the date format for an expiration date token (see below) from appearing in an email as 2018-04-12 to April 12, 2018. How would I set up the Velocity Script to do so?

{{lead.Expiration Date:default=edit me}}

Thank you,

Laura Kimball

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

#set( $dateOptions = {

  "formats" : {

    "userin" : "yyyy-MM-dd",

    "userout" : "MMMM d, yyyy"

  },

  "timezones" : {

    "userin" : "America/New_York",

    "userout" : "America/New_York"

  },

  "locale" : $date.getLocale()

} )

#set( $expirationDatelike = $lead.ExpirationDate )

#set( $expirationDate = $convert.parseDate(

  $expirationDatelike,

  $dateOptions.formats.userin,

  $dateOptions.locale,

  $date.getTimeZone().getTimeZone($dateOptions.timezones.userin)

) )

#set( $expirationDate_formatted = $date.format(

  $dateOptions.formats.userout,

  $expirationDate,

  $dateOptions.locale,

  $date.getTimeZone().getTimeZone($dateOptions.timezones.userout)

) )

${expirationDate_formatted}

Note the very important inclusion of time zones (set the userin and userout time zone strings according to your tz).

You'll see a lot of examples (frankly, any examples that weren't originally contributed by yours truly) that are broken because they are timezone-unaware.  Those examples may seem simpler, but they're broken.

View solution in original post

15 REPLIES 15