I have two Ruby on Rails DateTime objects. How to find the number of months between them? (Keeping in mind they might belong to different years)
(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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With