SOLVED

Re: Format Date Token

Go to solution
Trinity_Levens3
Level 3

Hi, I need to change the standard date format from:  2017-02-27  to  27 February 2017   for an email send to a list of leads with a specific date for each. 

I'm using a token for a Contract Date field:  {{lead.Contract Date}} which produces the standard format 2017-02-27

The date in the lead record for the Contact Date field begins like this: Feb 27, 2017

Reading throughout the community on how to fix this and it's not as clear as I'd like but nonetheless I'm reaching out to see if there's a clear-cut answer as to how to code the velocity script and step by step instructions so it works.  I understand I can use a My.token at the program level and code it in velocity script, but from there I need more guidance.    Thanks!   -T

Tags (1)
1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

What time zone are those dates in (yes, this is an important detail when converting)?

View solution in original post

17 REPLIES 17
Grégoire_Miche2
Level 10
Trinity_Levens3
Level 3

Checking out these links, Yes the time zone is important as I'm reading what Sanford said and going to attempt to fix this myself based off the similar conversation another lady had, but my client is asking that I spell out the month...that seems a bit more involved, is there additional code to render each unique month into it's corresponding word?    Many Thanks!!!

SanfordWhiteman
Level 10 - Community Moderator

Date format macro for the full month is MMMM. Thus when outputting the date/calendar object $contractDate:

${date.format("d MMMM yyyy",$contractDate)}

Trinity_Levens3
Level 3

Sanford Whiteman​  I tried replacing the last line of code with this:

${date.format("d MMMM yyyy",$contractDate)} 

But it rendered incorrect: ${date.format("d MMMM yyyy",$contractDate)} , and I'm too curious not to understand my mistake, what did I do wrong?

SanfordWhiteman
Level 10 - Community Moderator

Probably because you aren't using the variable name  $contractDate. You'd replace that with the actual variable name declared earlier in your code.

Trinity_Levens3
Level 3

Hmm sorry Sanford not sure about that, I'm still learning and I tried something and it didn't work, thus I keep trying

Trinity_Levens3
Level 3

Works great for this format:  Monday, February 27, 2017

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

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

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

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

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

SanfordWhiteman
Level 10 - Community Moderator

Sure, that looks like my go-to code there.

The line that begins

${date.format(...

uses variables declared earlier ($myDate, $locale, etc.).

Trinity_Levens3
Level 3

Sanford Whiteman​   I'm getting a nasty error when I use this: pastedImage_0.png

Why can't I be as smart as you?!?!  I just want to know the answer!! 

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

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

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

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

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

SanfordWhiteman
Level 10 - Community Moderator

You put the code in an Email Script (Velocity) token, right?

Trinity_Levens3
Level 3

Hi Sanford,  Yes the velocity email script token was used, just replaced the code.  

SanfordWhiteman
Level 10 - Community Moderator

Can you show me a screenshot of the Email Script Editor with your code in it?

And you're sure (a) you have Contract Date selected in the right-hand tree and (b) that its VTL variable name is $lead.contractName? I would expect it to be $lead.Contract_Name given Velocity naming conventions.

Trinity_Levens3
Level 3

the Contract Date field value is:  ${lead.contractDate}

pastedImage_0.png

Trinity_Levens3
Level 3

this is the current script for this format:  Monday, February 27, 2017    I replaced the error script so that I can get this email out as it's a rush for today

Trinity_Levens3
Level 3

Ok thank you I just tried the other way from the third link that Greg provided and it worked!!

Monday, February 27, 2017

I'm going to try this new code you just provided but I think my internal client will accept either option, thanks to you both, always appreciated how helpful and quick you are!!!

SanfordWhiteman
Level 10 - Community Moderator

What time zone are those dates in (yes, this is an important detail when converting)?

Trinity_Levens3
Level 3

Central Standard Time