Return Last 4 months using velocity | Community
Skip to main content
Level 2
August 13, 2024
Solved

Return Last 4 months using velocity

  • August 13, 2024
  • 1 reply
  • 1686 views

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

 

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

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.

 

1 reply

SanfordWhiteman
Level 10
August 13, 2024

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.
Level 2
August 14, 2024

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
SanfordWhitemanAccepted solution
Level 10
August 14, 2024

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.