Skip to main content
Alan_Harris1
Level 2
November 2, 2018
Question

Adding days to a date field with Velocity script

  • November 2, 2018
  • 1 reply
  • 12125 views

I've tried all the techniques I've seen online for adding or subtracting hours / days / months / years to a date using Velocity scripting, but have bee unable to successfully recreate it myself.

I'm using the following email script to take a date in "MM-DD-YYYY" from a Salesforce opportunity date field, and output as "Day of week, Date, Year". For example 11/02/2018 would be outputted as "Friday, November 2, 2018".

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

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

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

#set( $myDate = $convert.parseDate($OpportunityList.get(0).Auto_Renewal_Date__c,'yyyy-MM-dd',$locale,$inTimeZone) )

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

The script above works, but I'd like to also be able to add either 30 days to the displayed date, or add 1 month. So for example, 11-02-2018 would output as "Sunday, December 2, 2018"

Nothing I've tried has worked so far. Any help would be greatly appreciated.

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

1 reply

SanfordWhiteman
Level 10
November 2, 2018

See the 7-days-from-today example at https://blog.teknkl.com/velocity-days-and-weeks/

1 month is MONTH,1 in place of DATE,7.

Alan_Harris1
Level 2
November 2, 2018

Thanks Sanford. That "7 days from today" example allowed me to in fact display the date 7 days from today's date. How do I modify it to display 7 days from the date in my variable $OpportunityList.get(0).Auto_Renewal_Date__c, ?

SanfordWhiteman
Level 10
November 2, 2018

In the expression

$calNow.add($calConst.DATE,7)

$calNow is a Calendar object. It happens to be set to the current timestamp, but whatever Calendar you create will work. If you already have a Date you can do

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

then

$myCal.add($calConst.DATE,7)  

I'm hesitant to approve of using the first Opportunity on the lead (index 0) all the time. Are you sure that's what you want?