SOLVED

Change Date Format Using Velocity Script and Tokens

Go to solution
Scott-Bush
Level 1

Change Date Format Using Velocity Script and Tokens

I'm stumped. Been through many articles, even tried ChatGPT to find Velocity Script that changes the date format on a token in an email. I do have an Account field that pulls in the date successfully: {{Company.Renewal Date}}, but in the Mkto standard yyyy-MM-dd, or 2023-12-22. I want it to be MMMM dd, yyyy or December 22, 2023. Velocity script doesn't seem to support Account fields so I had my Sales Ops create a Contact field, {{lead.Account Renewal Date}} to pull in from the Account field. I CAN see this field in the VS objects list. My referring "my.token" in this case is {{my.RenewalDate}} and it "tries" to use the following VS to no avail. 

Appreciate any feedback for fix suggestions.

#set( $dateOptions = {
"formats" : {
"userin" : "yyyy-MM-dd HH:mm:ss",
"userout" : "MMMM d, yyyy 'at' HH:mm:ss z"
},
"timezones" : {
"userin" : "America/New_York",
"userout" : "America/New_York"
},
"locale" : $date.getLocale()
})

#set( $renewalDatelike = $lead.account_renewal_date__c )

#set( $renewalDate = $convert.parseDate(
$renewalDatelike,
$dateOptions.formats.userin,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userin)
) )

#set( $renewalDate_formatted = $date.format(
$dateOptions.formats.userout,
$renewalDate,
$dateOptions.locale,
$date.getTimeZone().getTimeZone($dateOptions.timezones.userout)
) )

$renewalDate_formatted

 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Change Date Format Using Velocity Script and Tokens

You shouldn’t be using formal notation here:

 

#set( $myDate = $convert.parseDate(${lead.Your Token Here},'yyyy-MM-dd',$locale,$inTimeZone) )

 

Always use simple notation unless you’re in an output context:

 

#set( $myDate = $convert.parseDate($lead.Your_Field_Here,'yyyy-MM-dd',$locale,$inTimeZone) )

 

Also, it’s not a “token” in Velocity but the Velocity field name, so I changed the snippet accordingly.

View solution in original post

8 REPLIES 8
SanfordWhiteman
Level 10 - Community Moderator

Re: Change Date Format Using Velocity Script and Tokens

Please remember to use the Syntax Highlighter (“Insert/Edit Code Sample”) so code is readable. I edited your post this time.

 

The problem is your formats don’t correctly describe your input and output.

 

The input is an ISO Date (yyyy-MM-dd) and your desired output doesn’t include the time, so you want:

#set( $dateOptions = {
  "formats" : {
    "userin" : "yyyy-MM-dd",
    "userout" : "MMMM d, yyyy"
  },
  "timezones" : {
    "userin" : "America/New_York",
    "userout" : "America/New_York"
  },
  "locale" : $date.getLocale()
})

 

Scott-Bush
Level 1

Re: Change Date Format Using Velocity Script and Tokens

In a preview test, the output is $renewalDate_formatted - it didn't express the date.

SanfordWhiteman
Level 10 - Community Moderator

Re: Change Date Format Using Velocity Script and Tokens

Gonna need more info than that. Note you have to use Preview By List to correctly target a lead.

 

Please show the output of

 

${lead}

 

 

When I set $lead.account_renewal_date__c to a standard ISO date (yyyy-MM-dd) the output looks fine.

jsiebert
Level 3

Re: Change Date Format Using Velocity Script and Tokens

@Scott-Bush - I did the same thing for a project, and parsed the date in much smaller code. I don't mind sharing - hope this is useful! (I am not familiar with VS best practices, so I'm hoping I can learn some by sharing my code with others! All I know is that this works 😁)

 

#set( $inTimeZone = $date.getTimeZone().getTimeZone('America/New_York') ) 

#set( $outTimeZone = $date.getTimeZone().getTimeZone('America/New_York') ) 

#set( $locale = $date.getLocale() ) 

#set( $myDate = $convert.parseDate(${lead.Your Token Here},'yyyy-MM-dd',$locale,$inTimeZone) ) 

${date.format('MMMM dd, yyyy',$myDate,$locale,$outTimeZone)} 

 

Jack Siebert
Scott-Bush
Level 1

Re: Change Date Format Using Velocity Script and Tokens

@jsiebert That DOES work and it tested fine on both a Person view and List view. I had tried your code before but was using an account field - VS seems to require a lead field so I had Sales Ops create a Contact Field that pulls the Contact.Renewal Date into a lead.Account Renewal Date field. I never re-tested with that change. Thanks for jumping in and big thanks to @SanfordWhiteman for striving with me during the holiday season. Here's the code that works for me...

#set( $inTimeZone = $date.getTimeZone().getTimeZone('America/New_York') ) 

#set( $outTimeZone = $date.getTimeZone().getTimeZone('America/New_York') ) 

#set( $locale = $date.getLocale() ) 

#set( $myDate = $convert.parseDate(${lead.Account Renewal Date},'yyyy-MM-dd',$locale,$inTimeZone) ) 

${date.format('MMMM dd, yyyy',$myDate,$locale,$outTimeZone)} 

  

jsiebert
Level 3

Re: Change Date Format Using Velocity Script and Tokens

Great! I would make sure that it works when you send yourself a full e-mail (not through the 'Send Sample' capability). It should be fine, and the code works for me, but... measure twice cut once right? 😉

Jack Siebert
SanfordWhiteman
Level 10 - Community Moderator

Re: Change Date Format Using Velocity Script and Tokens

Also to be clear the 2 snippets are doing exactly the same thing once compiled by the Velocity engine.

 

The output works fine with my original fix as well.

SanfordWhiteman
Level 10 - Community Moderator

Re: Change Date Format Using Velocity Script and Tokens

You shouldn’t be using formal notation here:

 

#set( $myDate = $convert.parseDate(${lead.Your Token Here},'yyyy-MM-dd',$locale,$inTimeZone) )

 

Always use simple notation unless you’re in an output context:

 

#set( $myDate = $convert.parseDate($lead.Your_Field_Here,'yyyy-MM-dd',$locale,$inTimeZone) )

 

Also, it’s not a “token” in Velocity but the Velocity field name, so I changed the snippet accordingly.