From: neil@...9.co.uk (sentinel) Date: 2002-01-23T00:52:07+09:00 Subject: Re: Difficulties with dates / using the date.rb module One thing did occur to me when using the formula: mb=(ay-by)*12+(am-bm) This does calculate the number of months, but I sometimes need to take the days into account even though I am not calcualating the actual number of days. For instance: I have two dates 10 October 1989 1 October 1999 Using the formula above, this would show 120 months (or 10 years), whereas the answer I would need would be 119 whole months (or 9 years & 11 months) One way around this is to check to see if the dates are valid, and if so, if am = bm and ad < bd, then subtract 1 from mb. I suppose what I wanted to know is if I can use the built-in methods from date.rb to do the calculation ? Rgds Neil. Mathieu Bouchard wrote in message news:... > 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