[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Calculating the age given DOB

kristnjov

3/14/2007 2:48:00 PM

Hello, fellow rubyists.

I have a problem. I'm trying to write a simple program which calculates
the age of a person given the person's date of birth in an instance of a
Date class. I have no idea what is the best way to do this. I don't feel
like using any ugly "hacks" to do it, and I wonder if there is any good
(perhaps mathematical) algorithm out there to solve the problem.

--Deniz Dogan
23 Answers

Brian Adkins

3/14/2007 3:42:00 PM

0

Deniz Dogan wrote:
> Hello, fellow rubyists.
>
> I have a problem. I'm trying to write a simple program which calculates
> the age of a person given the person's date of birth in an instance of a
> Date class. I have no idea what is the best way to do this. I don't feel
> like using any ugly "hacks" to do it, and I wonder if there is any good
> (perhaps mathematical) algorithm out there to solve the problem.
>
> --Deniz Dogan


(DateTime.now - birthdate) / 365.25

kristnjov

3/14/2007 4:03:00 PM

0

Brian Adkins wrote:
> Deniz Dogan wrote:
>> Hello, fellow rubyists.
>>
>> I have a problem. I'm trying to write a simple program which
>> calculates the age of a person given the person's date of birth in an
>> instance of a Date class. I have no idea what is the best way to do
>> this. I don't feel like using any ugly "hacks" to do it, and I wonder
>> if there is any good (perhaps mathematical) algorithm out there to
>> solve the problem.
>>
>> --Deniz Dogan
>
>
> (DateTime.now - birthdate) / 365.25

And this would work with Date objects?

Clifford Heath

3/14/2007 4:08:00 PM

0

Brian Adkins wrote:
> (DateTime.now - birthdate) / 365.25

That's not very reliable. There are algorithms that calculate
the number of days since some epoch from a date, and the
reverse, based on Fortran code that was posted in the CACM
sometime around 1970. I have a copy somewhere, but there
must be a modern version already in Ruby. If you can't
find a way, I'll dig out mine.

Just convert both dates to days-since-epoch and subtract.

Clifford.

Brian Adkins

3/14/2007 4:32:00 PM

0

Clifford Heath wrote:
> Brian Adkins wrote:
>> (DateTime.now - birthdate) / 365.25
>
> That's not very reliable. There are algorithms that calculate
> the number of days since some epoch from a date, and the
> reverse, based on Fortran code that was posted in the CACM
> sometime around 1970. I have a copy somewhere, but there
> must be a modern version already in Ruby. If you can't
> find a way, I'll dig out mine.
>
> Just convert both dates to days-since-epoch and subtract.
>
> Clifford.

Do you mind if I ask why you make the statement, "That's not very
reliable." ? For what values of birthdate (an instance of Date) would
the above expression not be "reliable" ?

Brian Adkins

3/14/2007 4:33:00 PM

0

Deniz Dogan wrote:
> Brian Adkins wrote:
>> Deniz Dogan wrote:
>>> Hello, fellow rubyists.
>>>
>>> I have a problem. I'm trying to write a simple program which
>>> calculates the age of a person given the person's date of birth in an
>>> instance of a Date class. I have no idea what is the best way to do
>>> this. I don't feel like using any ugly "hacks" to do it, and I wonder
>>> if there is any good (perhaps mathematical) algorithm out there to
>>> solve the problem.
>>>
>>> --Deniz Dogan
>>
>>
>> (DateTime.now - birthdate) / 365.25
>
> And this would work with Date objects?

Why don't you give it a try?

Reid Thompson

3/14/2007 4:34:00 PM

0

On Wed, 2007-03-14 at 23:55 +0900, Deniz Dogan wrote:
> Hello, fellow rubyists.
>
> I have a problem. I'm trying to write a simple program which calculates
> the age of a person given the person's date of birth in an instance of a
> Date class. I have no idea what is the best way to do this. I don't feel
> like using any ugly "hacks" to do it, and I wonder if there is any good
> (perhaps mathematical) algorithm out there to solve the problem.
>
> --Deniz Dogan
>
rthompso@jhereg:~$ irb
irb(main):001:0> require 'date'
=> true
irb(main):002:0> bd = Date.new(1966, 1, 21)
=> #<Date: 4878293/2,0,2299161>
irb(main):003:0> today = Date.new(2007,3,14)
=> #<Date: 4908347/2,0,2299161>
irb(main):004:0> age = today.year - bd.year
=> 41


Brian Adkins

3/14/2007 4:42:00 PM

0

Reid Thompson wrote:
> On Wed, 2007-03-14 at 23:55 +0900, Deniz Dogan wrote:
>> Hello, fellow rubyists.
>>
>> I have a problem. I'm trying to write a simple program which calculates
>> the age of a person given the person's date of birth in an instance of a
>> Date class. I have no idea what is the best way to do this. I don't feel
>> like using any ugly "hacks" to do it, and I wonder if there is any good
>> (perhaps mathematical) algorithm out there to solve the problem.
>>
>> --Deniz Dogan
>>
> rthompso@jhereg:~$ irb
> irb(main):001:0> require 'date'
> => true
> irb(main):002:0> bd = Date.new(1966, 1, 21)
> => #<Date: 4878293/2,0,2299161>
> irb(main):003:0> today = Date.new(2007,3,14)
> => #<Date: 4908347/2,0,2299161>
> irb(main):004:0> age = today.year - bd.year
> => 41
>
>

Consider when today.month < bd.month - you'll have an off-by-one error.

Clifford Heath

3/14/2007 4:46:00 PM

0

Brian Adkins wrote:
> Clifford Heath wrote:
>> Brian Adkins wrote:
>>> (DateTime.now - birthdate) / 365.25
> Do you mind if I ask why you make the statement, "That's not very
> reliable." ?

I have written genealogy software, and sometimes I want to know
how old someone was on a given date, like when they married.
1900 was not a leap year, as I'm sure you know. Neither was
1800 or 1700, but 1600 was. Does that make more sense now?

If I write a "library function" like this, I like to be a stickler,
because it's usually myself that gets tripped up.

Pit Capitain

3/14/2007 5:15:00 PM

0

Brian Adkins schrieb:
> Reid Thompson wrote:
>> irb(main):001:0> require 'date'
>> => true
>> irb(main):002:0> bd = Date.new(1966, 1, 21)
>> => #<Date: 4878293/2,0,2299161>
>> irb(main):003:0> today = Date.new(2007,3,14)
>> => #<Date: 4908347/2,0,2299161>
>> 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


John Joyce

3/14/2007 5:27:00 PM

0

I think we can honestly assume that if you are born on leap day, you
become highly tolerant of systems that don't even accept leap day
birthdays and probably write Feb 28 or Mar 1 consistently by habit.
On Mar 15, 2007, at 2:14 AM, Pit Capitain wrote:

> Brian Adkins schrieb:
>> Reid Thompson wrote:
>>> irb(main):001:0> require 'date'
>>> => true
>>> irb(main):002:0> bd = Date.new(1966, 1, 21)
>>> => #<Date: 4878293/2,0,2299161>
>>> irb(main):003:0> today = Date.new(2007,3,14)
>>> => #<Date: 4908347/2,0,2299161>
>>> 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
>
>