SOLVED

Re: How to reformat date Token field as full date?

Go to solution
Anonymous
Not applicable

How to reformat date Token field as full date?

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

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: How to reformat date Token field as full date?

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

Re: How to reformat date Token field as full date?

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

Re: How to reformat date Token field as full date?

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.

SanfordWhiteman
Level 10 - Community Moderator

Re: How to reformat date Token field as full date?

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

Re: How to reformat date Token field as full date?

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

Re: How to reformat date Token field as full date?

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

Re: How to reformat date Token field as full date?

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

Re: How to reformat date Token field as full date?

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

Re: How to reformat date Token field as full date?

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

Re: How to reformat date Token field as full date?

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