SOLVED

Need help with Velocity Script - Changing date format

Go to solution
TLafferty
Level 1

Need help with Velocity Script - Changing date format

I have a customer token, {{my.SupportCase_CreatedDate}}, that displays as yyyy-M-d H:m:s (example: 2023-01-05 12:26:14) when I would rather it display as MMMM d, yyyy (example: January 5, 2023).

 

I'm new to Marketo and scripting so any help would be greatly appreciated. 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Need help with Velocity Script - Changing date format


Correct, it is a text my.token.

No, it’s not! You’re describing an existing Velocity {{my.token}} that’s reading a field from Custom Object record.

 

Now first: using .get(0) on a list is never recommended. You should consider the list to be unordered, so getting the 0th item (i.e. the first item) is getting a random item. Explicitly sorting and filtering, then locating the correct focused record, is the correct practice.

 

Assuming you get past that and have a date-like String field, you need to convert it to an actual Date using the methods illustrated here.

 

The output format string for “January 5, 2023” is indeed MMMM d, yyyy.

 

But input format you’re using isn’t really yyyy-M-d H:m:s as you typed. It’s yyyy-MM-dd HH:mm:ss, which is called $ISO8601DateTimeWithSpace in the post. (Though technically it’s not valid ISO 8601 because of the space, and I should’ve called it $NonISODateTimeWithSpace.)

View solution in original post

3 REPLIES 3
SanfordWhiteman
Level 10 - Community Moderator

Re: Need help with Velocity Script - Changing date format

This {{my.token}} is a Text token, right?

 

If so — not that I don’t appreciate using a sort-of-like-standard-ISO input format when populating data — why not enter it using the desired output format?

 

You can’t read another non-Velocity {{my.token}} from a Velocity token. So if you wanted to change the format, you’d need to initially enter the value in its own Velocity {{my.token}}, e.g.

#set( $SupportCase_CreatedDate = "2023-01-05T12:26:14Z" )

 

Then you can reformat $SupportCase_CreatedDate in another Velocity {{my.token}}.

TLafferty
Level 1

Re: Need help with Velocity Script - Changing date format

@SanfordWhiteman - Correct, it is a text my.token. I have no control over the input values unfortunately. Just trying to get them to display in a preferred format if at all possible.

 

This is the field that is within the my.token: ${CaseList.get(0).CreatedDate}

Is this more helpful?

 

Appreciate your help!

SanfordWhiteman
Level 10 - Community Moderator

Re: Need help with Velocity Script - Changing date format


Correct, it is a text my.token.

No, it’s not! You’re describing an existing Velocity {{my.token}} that’s reading a field from Custom Object record.

 

Now first: using .get(0) on a list is never recommended. You should consider the list to be unordered, so getting the 0th item (i.e. the first item) is getting a random item. Explicitly sorting and filtering, then locating the correct focused record, is the correct practice.

 

Assuming you get past that and have a date-like String field, you need to convert it to an actual Date using the methods illustrated here.

 

The output format string for “January 5, 2023” is indeed MMMM d, yyyy.

 

But input format you’re using isn’t really yyyy-M-d H:m:s as you typed. It’s yyyy-MM-dd HH:mm:ss, which is called $ISO8601DateTimeWithSpace in the post. (Though technically it’s not valid ISO 8601 because of the space, and I should’ve called it $NonISODateTimeWithSpace.)