SOLVED

Velocity adding years to a date token

Go to solution
Travis_Schwartz
Level 4

Velocity adding years to a date token

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?

2 ACCEPTED SOLUTIONS

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity adding years to a date token

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

View solution in original post

Jo_Pitts1
Level 10 - Community Advisor

Re: Velocity adding years to a date token

@Travis_Schwartz ,

I may be being very dense here (I often am), but where is all the $dateOptions stuff coming from?  I don't see it set anywhere.  

 

I see @SanfordWhiteman has written about it here https://nation.marketo.com/t5/product-discussions/how-do-i-modify-the-date-format-for-a-token-using-...

 

Maybe you've missed a step in your code?

 

Cheers

Jo

 

 

 

View solution in original post

6 REPLIES 6
SanfordWhiteman
Level 10 - Community Moderator

Re: Velocity adding years to a date token

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

Travis_Schwartz
Level 4

Re: Velocity adding years to a date token

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
Level 4

Re: Velocity adding years to a date token

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}




Jo_Pitts1
Level 10 - Community Advisor

Re: Velocity adding years to a date token

@Travis_Schwartz ,

I may be being very dense here (I often am), but where is all the $dateOptions stuff coming from?  I don't see it set anywhere.  

 

I see @SanfordWhiteman has written about it here https://nation.marketo.com/t5/product-discussions/how-do-i-modify-the-date-format-for-a-token-using-...

 

Maybe you've missed a step in your code?

 

Cheers

Jo

 

 

 

Travis_Schwartz
Level 4

Re: Velocity adding years to a date token

haha no problem at all. it is likely the issue with the script... I'm not entirely sure where I picked it up along the way (I've been looking at a lot of posts and things trying to piece this together). 

If you have any solutions, I'm all ears.

I had seen that post and was trying to figure out how I would implement my custom list data into it, but was not successful.

Travis_Schwartz
Level 4

Re: Velocity adding years to a date token

@Jo_Pitts1, thanks again for pointing this out. 

I went back to the drawing board and @SanfordWhiteman's blog post and I am now getting the desired result with the following script:

#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( $FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ = "MMMM dd, yyyy" )

#set( $targetProductTypes = [3022, 3023, 3024, 3025, 3026, 3027, 3028, 3029, 3030, 3031, 3042, 3043, 3044, 3045, 3046, 3047, 3048, 3049] )

#foreach($customList in $account_cList)
    #if($targetProductTypes.contains($customList.productType) && $customList.openDate && !$customList.openDate.isEmpty())
        #set($myDate = $convert.parseDate($customList.openDate, "yyyy-MM-dd", $defaultLocale, $defaultTimeZone))
        
        $calNow.setTime($myDate)
        $calNow.add($calConst.YEAR, 10)
        
        #set($formattedDate = ${date.format(
            $FRIENDLY_24H_DATETIME_WITH_FRIENDLY_TZ,
            $calNow,
            $defaultLocale,
            $defaultTimeZone
        )})
        
        ${formattedDate}, 
    #end
#end


One thing that I could resolve was that there was a space between ${formattedDate} and the comma... I moved the comma into the rendered part of the script and that solved the additional space. I didn't see anything in the HTML that would have caused it... so yeah... I think I'm good now...