irb(main):005:0> months[months.index(current), 42].size
not good if the app is a mortage calculator!
Sorry if this response is too late to be useful to anyone, but here’s a
solution that surprised me with its elegance. I read the OP and
immediately thought, “Oh, just spin forward 86,400 (seconds per day)
until the month changes” and, of course, someone had already posted this
reply. However, I wrote some code as a youth that played with dates
using division and moduli, and this post got me reminiscing on that.
Here’s a resurrected version. I was very pleased to discover it handles
negative numbers, which made writing Time#prev_month trivial and
def next_month n=1
t = Time::mktime year + Integer(n)/12, ((month+n-1)%12)+1
def prev_month n=1
Cheers, and thanks for asking an interesting question!