SOLVED

How to reformat date Token field as full date?

Go to solution
Anonymous
Not applicable

When I insert a token date field into an HTML email, the default format is yyyy-mm-dd.

Does anyone know how to reformat the date as a long date? Example: Sunday, October 19, 2003

I think I need to create a new Email Script Token in my Program, but I don't know how to write javascript.

Based on what I read here:

http://developers.marketo.com/documentation/velocity-script/script-examples/

I found some sample code (below).

I want to reformat this field:  lead.WarrantyExpirationDate

Desired format (long date): Sunday, October 19, 2003

I think the long date field is: $date.full_date

I know that I need to navigate to: Marketo > My Program > My Tokens > New > Email Script

and create a script for My Token {{WarrantyExpirationDate2}}.

I found these examples:

Example - Formatting a Date

Script Examples » Marketo Developers

##create a date and parse it with a format

##parseDate takes two args, a date string and the format

#set($myDate = $convert.parseDate("08-07-2015", "MM-dd-yyyy"))

##format the date

##format takes two args, the format and a date object

#set($formattedDate = $date.format("yyyy-MM-dd", $myDate))

${formattedDate}

I found another example here, but I don't know how to modify it for the long/full date format with my field names and I don't know which of these examples will work.

http://stackoverflow.com/questions/36182783/how-to-change-date-format-through-velocity-email-script-...

#set($myDate = $convert.parseDate(${lead.eventdate}, "dd-MM-yyyy"))
#set($formattedDate = $date.format("yyyy-MM-dd", $myDate))
${formattedDate}

Thanks in advance for your help!

1 ACCEPTED SOLUTION
SanfordWhiteman
Level 10 - Community Moderator

I'm in Eastern time zone if that helps

Obviously it helps.  I don't know why you can't see how critical that is. Every time you look at a clock you're using timezones. What you may not realize is that when you ask a computer, "Tell me the current day of the week," if you don't specifically say "Tell me the current day of the week in New York City" it may tell you the current day of the week in wherever it thinks it's physically located, or the current day of the week in Greenwich, England (source of UTC/GMT time). Plus, where the computer operates geographically (based on the audience it serves) might have nothing to do with where it is physically located, so even when you say "Tell me what time it is where you are" you can still get a totally wrong answer.

And even something as seemingly mind-numbingly simple and un-mess-uppable as "Tell me the year from the string '2016-09-30'" is not actually simple, since strings are not dates, and in order to convert them to dates in order to format them you need to... supply a time zone.

Anyway, assuming your input format is

     yyyy-MM-dd (note capital MM, not lowercase, is the 2-digit month)

To output in your format, use the following Velocity script:

#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.WarrantyExpirationDate,'yyyy-MM-dd',$locale,$inTimeZone) )

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

View solution in original post

16 REPLIES 16
SanfordWhiteman
Level 10 - Community Moderator

You shouldn't post to Champion Program as that's not a support area.  Please move to Products.

As far as your question, please check my answers on these two extremely recent community posts: Get time from timestamp Set local timezone using Velocity scripts

Also, Velocity isn't JavaScript.  It's a totally different special-purpose language.

Anonymous
Not applicable

It would be great if someone could post a script that actually works. All the suggestions that have been provided in other posts don't work. I wish Marketo would provide a way to reformat the date. Our audience is all in the U.S. They don't know whether 2016-09-07 is September of July.

Justin_Cooperm2
Level 10

If you want a US-formatted date (ex: "9/22/2016") for pacific time, here you go:

#set( $myDate = $date.getSystemDate() )

#set( $calendarObj = $convert.toCalendar($myDate) )

## Change Time Zone to Pacific (System Time is Central)

$calendarObj.add(10,-2)

#set( $output = $date.format('M/d/yyyy', $calendarObj ) )

${output}

Anonymous
Not applicable

Thanks, this works for me too

SanfordWhiteman
Level 10 - Community Moderator

I didn't link you to non-working code. That code works perfectly for the input provided. And any post that doesn't include timezone information is explicitly wrong. And yes, all Velocity dates have timezones. Even dates displayed only as a YYYY year have timezones that affect accuracy.

You haven't shown the code you're currently using, nor the exact output you're expecting (including timezones at both ends). It's impossible to build for an ambiguous requirement.

And Marketo does give you a very, very flexible way to format dates with Velocity. It must be flexible because it can accommodate any input and output combination (for example, 9/20/2016 to 2016-Sep-09).  But it cannot be magical.  If you don't know the actual input and output you want, you can't possibly change it to anythng predictable.

Anonymous
Not applicable

I want to reformat this field:  lead.WarrantyExpirationDate

Desired format (long date): Sunday, October 19, 2003

I don't know javascript at all.  What script can apply that will work result in the long date?

SanfordWhiteman
Level 10 - Community Moderator

Do you understand that I am asking you about the input and output timezone? I thought I was clear about that requirement.

Also, no JavaScript here.  As I said up top, Velocity and JavaScript have almost nothing in common.

Anonymous
Not applicable

I don't understand javascript or velocity scripts. I don't understand the input/output timezone info. I'm in Eastern time zone if that helps.

I just need code I can copy and paste. Unfortunately, Marketo Support won't help with this. If this is ever resolved, I hope this thread will help other people struggling with reformatting the unusuable yyyy-mm-dd format.

If you or anyone is able to help, I would greatly appreciate it.

SanfordWhiteman
Level 10 - Community Moderator

I'm in Eastern time zone if that helps

Obviously it helps.  I don't know why you can't see how critical that is. Every time you look at a clock you're using timezones. What you may not realize is that when you ask a computer, "Tell me the current day of the week," if you don't specifically say "Tell me the current day of the week in New York City" it may tell you the current day of the week in wherever it thinks it's physically located, or the current day of the week in Greenwich, England (source of UTC/GMT time). Plus, where the computer operates geographically (based on the audience it serves) might have nothing to do with where it is physically located, so even when you say "Tell me what time it is where you are" you can still get a totally wrong answer.

And even something as seemingly mind-numbingly simple and un-mess-uppable as "Tell me the year from the string '2016-09-30'" is not actually simple, since strings are not dates, and in order to convert them to dates in order to format them you need to... supply a time zone.

Anyway, assuming your input format is

     yyyy-MM-dd (note capital MM, not lowercase, is the 2-digit month)

To output in your format, use the following Velocity script:

#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.WarrantyExpirationDate,'yyyy-MM-dd',$locale,$inTimeZone) )

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

Anonymous
Not applicable

I copied your 5 lines of code to:  Program > My Tokens > my.WarrantyExpirationDateFormatted

I inserted the token in the email body:

{{my.WarrantyExpirationDateFormatted:default=}}

The sample email displayed this:

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

Did I do something wrong?

SanfordWhiteman
Level 10 - Community Moderator

Make sure you have checked off WarrantyExpirationDate in the tree of fields on the right side of the Email Script Editor popup.

Anonymous
Not applicable

The corresponding field was checked.

SanfordWhiteman
Level 10 - Community Moderator

Let's check to see what the actual name of the field is in Velocity.  You've said it was Lead.WarrantyExpirationDate.  If you start a new Email Script token (just to leave the real one alone), check the field, and drag the field into the left pane, what shows up?

Anonymous
Not applicable

The "W" is lower case when I drag the field over in the test token. I changed it to lower case in the actual token and IT WORKED!!!

THANK YOU SO MUCH!!!!!

SanfordWhiteman
Level 10 - Community Moderator

Great. Pls mark my answer with the code as Correct for future searches.

As you can see, there are several moving parts to date parsing and formatting, but once you have covered every point and presented it to the machine, there will (nearly) always be a solution. The fundamental problem is one of human communication vs. machine: machines either make zero assumptions or wrong assumptions about the meaning of times and dates, while people frequently exchange shorthand, like leaving off "am" or "pm" or using fuzzy terms like "next Tuesday" to which the listener unconsciously adds other context to make sense (although that can cause occasional problems in the real world, too).

If you're ever interviewing developers (however unlikely this may be!) and someone says dates/locales/timezones are easy, don't hire them, since they're lying about their experience!

Anonymous
Not applicable

I marked your Reply with the code as the correct answer. However, the only correction would be making Warranty lower case in your code. Thanks for teaching me that It's important to drag and drop the date field from the list on the right into the code to ensure it is correct.