Recently, I was tasked with creating a calendar which transitioned from month to month such as December to January to February, etc.. Also, I needed to go backward on the transition.
A good way to do this is to use a modulo operator. This means that you can figure out which month is the next month number very easily.
So if the month is December, which is month number 12, to find out which month is next:
month = (month % 12) + 1 or (12 % 12 )+ 1, this will work for any month number from 1 to 12.
BUT to go backwards on the month numbers, here is the operation:
In Ruby, here is the equivalent backward operation:
month = ((month-2) % 12) + 1