Computing the age of a person?

Devin Asay devin_asay at byu.edu
Thu May 14 12:49:28 EDT 2009


On May 14, 2009, at 10:37 AM, J. Landman Gay wrote:
>
> Good catch. Yes, it should be what you say. So much for quick  
> solutions. :)

Don't feel bad. My own attempt was a wash. I was using seconds to  
calculate the time from the beginning of the birth year to the  
birthdate, then comparing that to the beginning of the present year to  
the present date. It worked okay as long as the birthdate was > 1970  
(i.e., start of epoch for Unix). For birthdays before 1970 this method  
doesn't work exactly right. For example, my birthday is in 1956 and  
comparing the birthday to a similar date in this year I was always off  
by a day or two (probably due to leap years?) I was puzzling over this  
when Jacque's solution came up. I liked hers a lot better.

Here's my code in case anyone is interested:

function calcAge pBday
    put pBday into tBday
    convert tBday to dateitems
    put item 1 of tBday into tBirthYear
    convert tBday to seconds

    convert the date to dateitems
    put item 1 of it into tCurrYear
    put it into tCurrDate
    convert tCurrDate to seconds

    put tBirthYear,1,1,0,0,0,0 into tBirthYearStartSeconds
    convert tBirthYearStartSeconds to seconds
    get the result
    put tCurrYear,1,1,0,0,0,0 into tCurrYearStartSeconds
    convert tCurrYearStartSeconds to seconds

    if (tBDay - tBirthYearStartSeconds) <= (tCurrDate -  
tCurrYearStartSeconds) then
       return tCurrYear - tBirthYear
    else
       return tCurrYear - tBirthYear - 1 into fld "age"
    end if
end calcAge

Now we'll wait for Sarah R. to come on board and show us how it's  
really done!

Devin

Devin Asay
Humanities Technology and Research Support Center
Brigham Young University




More information about the use-livecode mailing list