Find number of months between two Dates in Ruby on Rails Find number of months between two Dates in Ruby on Rails ruby ruby

Find number of months between two Dates in Ruby on Rails


(date2.year * 12 + date2.month) - (date1.year * 12 + date1.month)

more info at http://www.ruby-forum.com/topic/72120


A more accurate answer would consider days in the distance.

For example, if you consider that the month-distance from 28/4/2000 and 1/5/2000 is 0 rather than 1, then you can use:

(date2.year - date1.year) * 12 + date2.month - date1.month - (date2.day >= date1.day ? 0 : 1)


Give a try to

((date2.to_time - date1.to_time)/1.month.second).to_i