Computing the age of a person?

Kay C Lan lan.kc.macmail at gmail.com
Fri May 15 02:05:17 EDT 2009


As some people probably want a more accurate representation of age rather
than in whole years, here's another function, this outputs yy,mm,dd as in
21,5,26.

The really cool bit about this is from my meager testing it appears to take
into account leap years. This is based around a cool piece of Rev
functionality, if you enter a date, convert it to dateItems, then put 0 into
item 3 of dateItems, convert the date again to dateItems, the result is the
last day of the previous month.

To test this, put a birth date like 3/15/50 and test against a date like
3/14/09, the answer will be 59,11,27. If you change the comparison date to
3/15/09 the answer is 60,0,0. ie someone born in March can never be more
than yy,11,27 or 28.

Again not fully tested so please feel free to find the errors of my ways.

FUNCTION calcAccAge 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 tYrMthday
    put item 2 of tAsOf - item 2 of tBirthdate into item 2 of tYrMthDay
    put item 3 of tAsOf - item 3 of tBirthdate into item 3 of tYrMthday
    IF (item 3 of tYrMthDay < 0) THEN
        put tAsOf into tHowManyDays
        --here's the cool bit. 0 in item 3 of date items effectively gives
you how many days in the month before
        put 0 into item 3 of tHowManyDays
        convert tHowManyDays to dateItems
        put item 3 of tHowManyDays + item 3 of tYrMthDay into item 3 of
tYrMthDay
        --when days are - we need to subtact 1 from the month count
        put item 2 of tYrMthDay - 1 into item 2 of tYrMthDay
    END IF
    IF (item 2 of tYrMthDay < 0) THEN
        --used to convert a negative month count into a positive number
        put 12 + item 2 of tYrMthDay into item 2 of tYrMthDay
        --when month is negative need to subtact 1 from yr count
        put item 1 of tYrMthDay - 1 into item 1 of tYrMthDay
    END IF
    return tYrMthDay
END calcAccAge



More information about the use-livecode mailing list