From: Brian Candler Date: 2007-03-16T15:15:06+09:00 Subject: Re: Calculating the age given DOB On Fri, Mar 16, 2007 at 04:28:16AM +0900, Rick DeNatale wrote: > rick@frodo:/public/rubyscripts$ cat datemath.rb > #!/usr/math/bin/ruby > > > require 'date' > class Date > > # return the number of days since the beginning of the year > def years_since(date) > # The parens in the expression below aren't strictly necessary, but > # I think it makes what's going on a little bit clearer. > first, last = *(self >= date ? [date, self] : [self, date]) > (self <=> date) * ((last.year - first.year) - (first.yday > > last.yday ? 1 : 0)) > end > end I guess the most generic solution would return the number of [years, months, days] between two dates - so you can say "You are 31 years, 3 months and 4 days old" Anyone can work out their own age in this format, so the algorithm should be clear, albeit probably messy to implement.