Velocity adding years to a date token | Community
Skip to main content
Travis_Schwartz
New Participant
July 28, 2023
Solved

Velocity adding years to a date token

  • July 28, 2023
  • 1 reply
  • 3193 views

I have a date field that I am pulling from a custom list, and I'm getting the date expected, but I want to take that date and add ten years to it. So I have the beginning date, and I want to create a field that shows the end date which is ten years after the opening.

I've not been able to figure out how to add the ten years. It feels like it should be relatively simple, but have not been successful.

#set( $dateOptions = { "formats" : { "userin" : "yyyy-MM-dd", "userout" : "MMM d, yyyy" }, "timezones" : { "userin" : "America/Los_Angeles", "userout" : "America/Los_Angeles" }, "locale" : $date.getLocale() } ) #set($myDate_formatted = "") #foreach($customList in $account_cList) #if($customList.productType == 3044 && !$customList.openDate.isEmpty()) #set($myDatelike = $customList.openDate) #set($myDate = $convert.parseDate($myDatelike, $dateOptions.formats.userin, $dateOptions.locale, $date.getTimeZone().getTimeZone($dateOptions.timezones.userin))) #set($myDate_formatted = $date.format($dateOptions.formats.userout, $myDate, $dateOptions.locale, $date.getTimeZone().getTimeZone($dateOptions.timezones.userout))) #break #end #end ${myDate_formatted}

So for example, if the date read "May 26, 2015" it would return a result of "May, 26, 2025" I'm sure there are things like leap years that would need to be accounted for and perhaps complicate it a little bit, but it feels like it should be doable. What changes need to occur to get this to operate correctly?

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 SanfordWhiteman

That's akin to this example but with $calConst.YEAR instead of $calConst.DATE.

1 reply

SanfordWhiteman
SanfordWhitemanAccepted solution
New Participant
July 28, 2023

That's akin to this example but with $calConst.YEAR instead of $calConst.DATE.

Travis_Schwartz
New Participant
July 28, 2023

Thank you! I looked at that post several times trying to find one that would match. I didn't know about the YEAR vs DATE switch.

 

Here is my updated script that is generating the desired result (if anyone wants to see it):

 

 

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Los_Angeles") ) #set( $defaultLocale = $date.getLocale() ) #set( $calNow = $date.getCalendar() ) #set( $ret = $calNow.setTimeZone($defaultTimeZone) ) #set( $calConst = $field.in($calNow) ) #set( $ISO8601DateOnly = "yyyy-MM-dd" ) #set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" ) #set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" ) #set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ) #set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) #set($myDate_formatted = "") #foreach($customList in $account_cList) #if($customList.productType == 3044 && !$customList.openDate.isEmpty()) #set($myDatelike = $customList.openDate) #set($myDate = $convert.parseDate($myDatelike, $dateOptions.formats.userin, $dateOptions.locale, $date.getTimeZone().getTimeZone($dateOptions.timezones.userin))) #set($myDate_formatted = $date.format($dateOptions.formats.userout, $myDate, $dateOptions.locale, $date.getTimeZone().getTimeZone($dateOptions.timezones.userout))) $calNow.add($calConst.YEAR, 10) #set( $FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ = "MMM dd, yyyy" ) ${date.format( $FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ, $calNow, $defaultLocale, $defaultTimeZone )} #break #end #end ${myDate_formatted}

 

Travis_Schwartz
New Participant
August 9, 2023

I spoke too soon.

The current script is adding 10 years to the current date, I need it to take the date in $customList.openDate and add 10 years.

 

Based on what I was seeing, it $calNow is taking today's date, so I tried changing it to $myDate (based on some digging) instead of $calNow, but that isn't working... This is where I'm getting stuck and I don't know what I need to change to fix it:

#set( $defaultTimeZone = $date.getTimeZone().getTimeZone("America/Los_Angeles") ) #set( $defaultLocale = $date.getLocale() ) #set( $calNow = $date.getCalendar() ) #set( $ret = $calNow.setTimeZone($defaultTimeZone) ) #set( $calConst = $field.in($calNow) ) #set( $ISO8601DateOnly = "yyyy-MM-dd" ) #set( $ISO8601DateTime = "yyyy-MM-dd'T'HH:mm:ss" ) #set( $ISO8601DateTimeWithSpace = "yyyy-MM-dd HH:mm:ss" ) #set( $ISO8601DateTimeWithMillisUTC = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'" ) #set( $ISO8601DateTimeWithMillisTZ = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" ) #set($myDate_formatted = "") #foreach($customList in $account_cList) #if($customList.productType == 3047 && !$customList.openDate.isEmpty()) #set($myDatelike = $customList.openDate) #set($myDate = $convert.parseDate($myDatelike, $dateOptions.formats.userin, $dateOptions.locale, $date.getTimeZone().getTimeZone($dateOptions.timezones.userin))) $myDate.add($calConst.YEAR, 10) #set( $FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ = "MMM dd, yyyy" ) ${date.format( $FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ, $myDate, $defaultLocale, $defaultTimeZone )} #break #end #end ${myDate_formatted}