From: Mathieu Bouchard Date: 2002-01-21T03:45:16+09:00 Subject: Re: Difficulties with dates / using the date.rb module On Fri, 18 Jan 2002, sentinel wrote: > Would anyone be able to help with the following date problem - I am > trying to calculate the years and months between two different dates. > Using the date.rb module I am able to calculate the years and days by > calculating the number of days from the start to the actual date, then > subtracting the smaller from the larger number and dividing by 365.25. > This is pretty accurate, but not what I actually need. > I did think about dividing the residual number of days by 30.4, but > I'm not convinced that this would give an accurate answer. I've read > the notes for the date module several times, but can't seem to work > this out. Is there a method of calculating the number of years and > whole calender months between two dates (or even just the total number > of calender months) that is not excessively complicated ? if you only worry about month boundaries, then you can get the year and month of both dates, ay, am, by, bm, and compute mb = (ay-by)*12 + (am-bm), the number of month boundaries crossed; you can separate into years (mb/12) and months (mb%12). if you need any fractionary value of months, then you're _not_ looking for month boundaries, and you have to deal with the issue that a month is not a fixed amount of time. You can do that by specifying that an average month is 30.437 and that should do it. In that case, you're really just looking for the number of days, and you are using the "month" as a mythical time unit. ;-) ________________________________________________________________ Mathieu Bouchard http://hostname.2y.net/~matju