SOLVED

Re: Return Last 4 months using velocity

Go to solution
bryantchang
Level 2

Return Last 4 months using velocity

Trying to return current month + last 4 months in 3 letter abbreviation using velocity.

This is what I have, but I'm sure there's a better way? πŸ˜…

 

#set( $calNow = $date.getCalendar() )
#set( $calNowMinus1 = $date.getCalendar() )
#set( $calNowMinus2 = $date.getCalendar() )
#set( $calNowMinus3 = $date.getCalendar() )
#set( $calNowMinus4 = $date.getCalendar() )
$calNowMinus1.add(2,-1)
$calNowMinus2.add(2,-2)
$calNowMinus3.add(2,-3)
$calNowMinus4.add(2,-4)
#set( $CurrentMonth = $date.format('MMM',$calNow) )
#set( $CurrentMonthMinus1 = $date.format('MMM',$calNowMinus1) )
#set( $CurrentMonthMinus2 = $date.format('MMM',$calNowMinus2) )
#set( $CurrentMonthMinus3 = $date.format('MMM',$calNowMinus3) )
#set( $CurrentMonthMinus4 = $date.format('MMM',$calNowMinus4) )

Current Month: $CurrentMonth<br>
Current Month Minus 1: $CurrentMonthMinus1<br>
Current Month Minus 2: $CurrentMonthMinus2<br>
Current Month Minus 3: $CurrentMonthMinus3<br>
Current Month Minus 4: $CurrentMonthMinus4

 

1 ACCEPTED SOLUTION

Accepted Solutions
SanfordWhiteman
Level 10 - Community Moderator

Re: Return Last 4 months using velocity

Apologies, that’ll teach me not to write code on a plane. Should be:

#set( $calNow = $date.getCalendar() )
#set( $calConst = $field.in($calNow) )
#set( $monthOffsets = [] )
#foreach( $offset in [1..5] )
#set( $void = $monthOffsets.add( $date.format("MMM", $calNow) ) )
#set( $void = $calNow.add($calConst.MONTH, -1) )
#end
${monthOffsets[0]}
${monthOffsets[1]}
## etc.

 

View solution in original post

5 REPLIES 5
SanfordWhiteman
Level 10 - Community Moderator

Re: Return Last 4 months using velocity

You don’t need the resource overhead of creating a new Calendar object each time if all you need is the stringified month. Also, using the Calendar constants make things a lot clearer!

#set( $calNow = $date.getCalendar() )
#set( $calConst = $field.in($calNow) )
#set( $monthOffsets = [] )
#foreach( $offset in [0..-4] )
#set( $void = $calNow.add($calConst.MONTH, $offset) )
#set( $void = $monthOffsets.add( $date.format("MMM", $calNow) ) )
#end
${monthOffsets[0]}
${monthOffsets[1]}
${monthOffsets[2]}
## etc.
bryantchang
Level 2

Re: Return Last 4 months using velocity

Thanks Sanford,

When I use the following,

#set( $calNow = $date.getCalendar() )
#set( $calConst = $field.in($calNow) )
#set( $monthOffsets = [] )
#foreach( $offset in [0..-4] )
#set( $void = $calNow.add($calConst.MONTH, $offset) )
#set( $void = $monthOffsets.add( $date.format("MMM", $calNow) ) )
#end

Current Month: ${monthOffsets[0]} <br>
Current Month Minus 1: ${monthOffsets[1]} <br>
Current Month Minus 2: ${monthOffsets[2]} <br>
Current Month Minus 3: ${monthOffsets[3]} <br>
Current Month Minus 4: ${monthOffsets[4]}

 

It seems to be incrementally add 1 additional month.

Returning the following output,

Current Month: Aug
Current Month Minus 1: Jul
Current Month Minus 2: May
Current Month Minus 3: Feb
Current Month Minus 4: Oct

 

When I am hoping to achieve the following,

Current Month: Aug
Current Month Minus 1: Jul
Current Month Minus 2: Jun
Current Month Minus 3: May
Current Month Minus 4: Apr

 

Something I missed?

SanfordWhiteman
Level 10 - Community Moderator

Re: Return Last 4 months using velocity

Apologies, that’ll teach me not to write code on a plane. Should be:

#set( $calNow = $date.getCalendar() )
#set( $calConst = $field.in($calNow) )
#set( $monthOffsets = [] )
#foreach( $offset in [1..5] )
#set( $void = $monthOffsets.add( $date.format("MMM", $calNow) ) )
#set( $void = $calNow.add($calConst.MONTH, -1) )
#end
${monthOffsets[0]}
${monthOffsets[1]}
## etc.

 

SanfordWhiteman
Level 10 - Community Moderator

Re: Return Last 4 months using velocity

@bryantchang please retest so we can close this out.

bryantchang
Level 2

Re: Return Last 4 months using velocity

Thanks @SanfordWhiteman !
Working as expected!