Computing the age of a person?

Kay C Lan lan.kc.macmail at gmail.com
Fri May 15 00:03:36 EDT 2009


Peter,

I gave your function a try and it gave the wrong answer. I had a quick look
and somewhere along the line it converted 2009 to 1909, so I ended up with a
negative age. After some of the earlier posts, I guess it has potential for
a female audience.

Here's my attempt:


FUNCTION calcAge tBirthdate,tAsOf
    IF tAsOf = "" THEN
        put the short date into tAsOf
    END IF
    convert tBirthdate to dateItems
    convert tAsOf to dateItems
    put item 1 of tAsOf - item 1 of tBirthdate into item 1 of tAsOf
    put item 2 of tAsOf - item 2 of tBirthdate into item 2 of tAsOf
    put item 3 of tAsOf - item 3 of tBirthdate into item 3 of tAsOf
    SWITCH
            --it's after the year you were born and after the month you were
born
        CASE ((item 1 of tAsOf > 0) AND (item 2 of tAsOf > 0))
            -- it's after the year, the same month, and the same day or
later
        CASE ((item 1 of tAsOf > 0) AND (item 2 of tAsOf = 0) AND (item 3 of
tAsOf >= 0))
            -- still in the same year
        CASE (item 1 of tAsOf = 0)
            return item 1 of tAsOf
            break
            -- a year after, but before your birth month
        CASE ((item 1 of tAsOf > 0) AND (item 2 of tAsOf < 0))
            --a year after, the same month, but before your birthday
        CASE ((item 1 of tAsOf > 0) AND (item 2 of tAsOf = 0) AND (item 3 of
tAsOf < 0))
            return (item 1 of tAsOf - 1)
            break
        DEFAULT
            --should only end up here if your tAsOf is before your
tBirthdate
            answer "Birthdate is after the Date you provided."
    END switch
END calcAge

Possible errors are people born in the early 1900s or before will have to
enter their dates with 4 digit years, otherwise Rev may assume the wrong
century.

On Fri, May 15, 2009 at 6:06 AM, Peter Brigham MD <pmbrig at gmail.com> wrote:

> Here's what I use:
>
> function doAge bd,asOf
>  -- bd = birthdate in short date form
>  -- asOf = (optional) short date
>  --    age is calculated as of date "asOf"
>  --    or as of today if omitted
>  if sws(bd) = "" then
>    return ""
>  end if
>  if asOf = "" then
>      put the short date into asOf
>  end if
>  -- first, get full year (yyyy) for each date
>  -- I found the centuryCutoff property unreliable
>  put getFullYr(bd) into actualBdYr
>  put getFullYr(asOf) into actualNowYr
>  convert bd to dateItems
>  put actualBdYr into item 1 of bd
>  convert asOf to dateItems
>  put actualNowYr into item 1 of asOf
>  put item 1 of bd into bYr
>  put item 2 of bd into bMo
>  put item 3 of bd into bDy
>  put item 1 of asOf into nYr
>  put item 2 of asOf into nMo
>  put item 3 of asOf into nDy
>  if bDy > nDy then subtract 1 from nMo
>  if bMo > nMo then subtract 1 from nYr
>  put nYr - bYr into age
>  return age
> end doAge
>
> function getFullYr tDate
>  -- works only for people < 100 years old
>  set the itemDelimiter to "/"
>  put the last item of tDate into yr
>  if the number of chars of yr = 2 then
>    if yr >= last item of the short date then
>      put "19" before yr
>    else
>      put "20" before yr
>    end if
>  end if
>  return yr
> end getFullYr
>
> function sws str
>  -- "strip whitespace"
>  return word 1 to -1 of str
> end sws
>
> ----------------------
>
> On 5/14/09, "klaus at major.on-rev.com"  wrote:
>
> Hi all,
>
> I could need a hint on how to compute the age of a person when I have the
> birthday. The "convert to seconds" and "subtract"ion part is easy, but how
> to proceed? :-)
>
> Thanks in advance!
>
> Best
>
> Klaus
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution
>



More information about the use-livecode mailing list