From: "F. Senault" Date: 2007-03-15T05:55:14+09:00 Subject: Re: Calculating the age given DOB Le 14 mars 2007 � 20:50, Pit Capitain a �crit : > F. Senault schrieb: >>>> (d1.year - d2.year) - (d1.yday >= d2.yday ? 0 : 1) > > Pardon Fred, using #yday would be short and nice, but this doesn't work > reliably if one year is a leap year and the other is not. Yup, you're perfectly right. The following version seems to work, but we're steadily losing elegance... :) >> d1 = Date.new(2007, 3, 1); d2 = Date.new(2004, 3, 1) => # >> (d1.year - d2.year) - (d1.yday >= Date.new(d1.year, d2.month, 1).yday + d2.day - 1 ? 0 : 1) => 3 >> d1 = d1 - 1 => # >> (d1.year - d2.year) - (d1.yday >= Date.new(d1.year, d2.month, 1).yday + d2.day - 1 ? 0 : 1) => 2 A pity Date.new doesn't handle overflows. I'd like to be able to write this : >> Date.new(2007, 2, 29).to_s => '2007-03-01' >> Date.new(2007, 3, 0).to_s => '2007-02-28' Fred -- And there shall be no more red roses, but only white, for she has squeezed the blood of all living things into her cup. (Aleister Crowley)