From: Pit Capitain Date: 2007-03-15T02:14:32+09:00 Subject: Re: Calculating the age given DOB Brian Adkins schrieb: > Reid Thompson wrote: >> irb(main):001:0> require 'date' >> => true >> irb(main):002:0> bd = Date.new(1966, 1, 21) >> => # >> irb(main):003:0> today = Date.new(2007,3,14) >> => # >> irb(main):004:0> age = today.year - bd.year >> => 41 > > Consider when today.month < bd.month - you'll have an off-by-one error. age -= 1 if today.month < bd.month || today.month == bd.month && today.day < bd.day I don't know how people born at February 29th celebrate their birthday in non-leap years. The code above makes them one year older on March 1st. Regards, Pit