Re: Adding days to a date field with Velocity script

Alan_Harris1
Level 1

Adding days to a date field with Velocity script

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.

11 REPLIES 11
SanfordWhiteman
Level 10 - Community Moderator

Re: Adding days to a date field with Velocity script

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 1

Re: Adding days to a date field with Velocity script

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 - Community Moderator

Re: Adding days to a date field with Velocity script

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?

Alan_Harris1
Level 1

Re: Adding days to a date field with Velocity script

Definitely not what I want, but unfortunately it's what I have to work with. The date I need to use as a trigger and as a dynamic personalization in the email is tied to an opportunity object. Thanks for the quick assistance! Will try this out.

SanfordWhiteman
Level 10 - Community Moderator

Re: Adding days to a date field with Velocity script

Why do you always have to use Opportunity 0? 

SanfordWhiteman
Level 10 - Community Moderator

Re: Adding days to a date field with Velocity script

Also please highlight code using the Advanced Editor's syntax highlighter so it's readable.

https://s3.amazonaws.com/blog-images-teknkl-com/syntax_highlighter.gif

Nathan_Hunter
Level 1

Re: Adding days to a date field with Velocity script

Thanks for this post Sanford. You've unlocked auto-expiry date for me (yet another thing on the long list of things you've solved for me).

SanfordWhiteman
Level 10 - Community Moderator

Re: Adding days to a date field with Velocity script

Awesome!

Jessica_Biblis4
Level 1

Re: Adding days to a date field with Velocity script

Thanks for sharing this post, Sanford. It's working great, but I am experiencing one issue that I can't figure out. In the plain text version of the email only, a line break is happening right before the date. It doesn't happen in the HTML version. Any thoughts on what might be causing this?

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/New_York") )
#set( $defaultLocale = $date.getLocale() )
#set( $calNow = $date.getCalendar() )
#set( $ret = $calNow.setTimeZone($defaultTimeZone) )
#set( $calConst = $field.in($calNow) )
#set( $ISO8601 = "yyyy-MM-dd'T'HH:mm:ss" )
#set( $ISO8601DateOnly = "yyyy-MM-dd" )
$calNow.add($calConst.DATE,6)
#define( $enUSDayOrdinalIndicators )
1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th 16th 17th 18th 19th 20th 21st 22nd 23rd 24th 25th 26th 27th 28th 29th 30th 31st
#end
#set( $indicatorList = $enUSDayOrdinalIndicators.toString().trim().split("\s?\d+") )
${date.format("EEEE, MMMM d'${indicatorList[$calNow.get($calConst.DAY_OF_MONTH)]}'",
$calNow,
$defaultLocale,
$defaultTimeZone
)}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍