Date Format Velocity Script | Community
Skip to main content
Jim_Clarke
New Participant
June 9, 2015
Solved

Date Format Velocity Script

  • June 9, 2015
  • 2 replies
  • 15041 views

Hello,

It's been a while since I formatted dates with Velocity, I'm struggling with the DateTool methods and was wondering if someone had an example.

Here's my snippet:

##sets timestamp variable

#set ( $NextAppointment = ${OpportunityList.get(0).NextPhoneCallAppointment__c})

##This displays the timestamp unformatted

$NextAppointment

##This should display the timestamp formatted

$date.format('medium',$NextAppointment)

My sample emails renders like this, clearly the method is working:

2015-06-18 17:30:00 $date.format('medium',$NextAppointment)

I was using this example to get to this state:

DateTool (VelocityTools 2.0-beta4 Documentation)

Any help would be greatly appreciated as to what I might be doing incorrectly on calling the method.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by

So with "medium" you're wanting something like Jun 18, 2015 5:30 PM?

If that's the case, something like this might work. I tested it with a similar field but don't know what format you're date was originally in:

$date.format('MMM dd, yyyy hh:mm aa', ${convert.parseDate(${OpportunityList.get(0).NextPhoneCallAppointment__c}), 'yyyy-MM-dd HH:mm:ss')})

2 replies

New Participant
June 16, 2017

Hey team

I'm trying something similar, but when I use the code given to Jim it's not working for me. Here's my code:

#set ($listlength = ${abandonedCart_cList.size()})

#set( $lastIndex = $math.sub($listlength,1) )

#set($str = $convert.parseDate(${abandonedCart_cList.get($lastIndex).serviceStartDate}, "yyyy-MM-dd HH:mm:ss")) 

$date.format('medium',$str) 

I only get the output $date.format('medium',$str)  instead of the formatted date. Why? Those last two lines are identical to what seemed to be working above, just with my variables substituted in.

Any ideas?

SanfordWhiteman
New Participant
June 17, 2017

Look for any of my posts where I show data parsing + display.

Make sure the date you're parsing really is in that string format, and debug each variable as you build it up. Plus wouldn't call the date variable $str as it's a Java Date at that point, not a String.
Accepted solution
June 9, 2015

So with "medium" you're wanting something like Jun 18, 2015 5:30 PM?

If that's the case, something like this might work. I tested it with a similar field but don't know what format you're date was originally in:

$date.format('MMM dd, yyyy hh:mm aa', ${convert.parseDate(${OpportunityList.get(0).NextPhoneCallAppointment__c}), 'yyyy-MM-dd HH:mm:ss')})

Jim_Clarke
New Participant
June 9, 2015

**Update**

Works like a charm I did one slight modification:

#set($str = $convert.parseDate(${OpportunityList.get(0).NextPhoneCallAppointment__c}, "yyyy-MM-dd HH:mm:ss"))

$date.format('medium',$str)

Ryan,

Thank you so much for the response, I really appreciate it.

Let me give it a try.

Jim