Use Email Script Token to Calculate Number of Years Based on a Date Field

Issue

How to use a script token to calculate and populate the number of years since a given date.

 


Solution

This can be achieved by using a velocity (email script) token:

 

#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" ) 

#set( $calJoinDate = $convert.toCalendar( 

$convert.parseDate( 

    $lead.JoinDate,  

    $ISO8601DateOnly,  

    $defaultLocale,  

    $defaultTimeZone  

  ) 

) ) 

#set( $differenceInYears = $date.difference($calJoinDate,$calNow ).getYears() ) 

#set( $friendlyLabel = $display.plural($convert.toInteger($differenceInYears),"year") ) 

You joined us ${differenceInYears} ${friendlyLabel} ago! 


Where $lead.JoinDate is the joining date. More at http://blog.teknkl.com/velocity-days-and-weeks/