Age & Anniversary Calculations

André Bisseret andre.bisseret at wanadoo.fr
Mon May 21 07:49:33 EDT 2012


Bonjour Igor,


Le 21 mai 2012 à 09:02, Igor de Oliveira Couto a écrit :

> Dear LC Folks,
> 
> What is the 'best' way to perform these 2 calculations with LiveCode?
> 
> 1) Calculate a person's age, given a birthdate:

I had problems with scripts mainly based on "convert to seconds" and "dateItems"
also, on windows, as Ken points it out, with birthday before 1970.

Eventually I wrote the following script (probably not the "best" ;-) but which works for months now, on Mac and Windows as well.

It minimizes the recourse to convert to seconds.
Notice that ages under one year, are returned in months.
As it is written, it requires 2 fields : a fld "fldBirthDay" and a fld "fldAge"

HTH

Best regards from Grenoble

André
--------
     local tBirthDate,tAge,tToday,tLivedSecs,t1stYearSecs,t1YearSecs,\
    tBirthDateSecs,tToDaySecs,tAddedMonths,date1,date2,tYearsNbr,tempo,
    ------------
on mouseUp
    lock screen
    -- ENGLISH DATE
    put fld "fldBirthDay" into tBirthDate
    put the short date into tToday
    set the itemDel to slash
    --------======IN CASE OF YY FOR YEAR FORMAT ======
    if the number of chars of item 3 of tBirthDate is 2 then
        if item 3 of tBirthDate > item 3 of tToday then
            put "19" before item 3 of tBirthDate
            put "19" before item 3 of fld "fldBirthDay"
        else
            put "20" & item 3 of tBirthDate into date1
            put "19" & item 3 of tBirthDate into date2
            answer "Sorry! can't decide ;-)" with date1 or date2
            put it into item 3 of fld "fldBirthDay"
            put it into item 3 of tBirthDate
        end if
    end if
    if the number of chars of item 3 of tToday is 2 then
        put "20" before item 3 of tToday
        -- put "20" before item 3 of fld "fldJourdhui"
    end if
    --------PRECAUTION FOR ENDS OF MONTH
    if item 1 of tToday is "2" and item 2 of tToday is among the items of "29/30/31" then
        put "28" into item 2 of tToday
    end if
    if item 1 of tToday is among the items of "4/6/9/11" then
        if item 2 of tToday is "31" then put "30" into item 2 of tToday
    end if
    ------------------------------------
    put item 3 of tToday - item 3 of tBirthDate into tYearsNbr
    -----------------
    -------- PUTTING IN MONTHS BEFORE 1 YEAR
    -- USING CONVERT TO SECONDS
    put tBirthDate into tBirthDateSecs
    put tToday into tToDaySecs
    convert tBirthDateSecs to seconds
    convert tToDaySecs to seconds
    put tBirthDate into t1stYearSecs 
    add 1 to item 3 of t1stYearSecs
    convert t1stYearSecs to seconds
    put t1stYearSecs - tBirthDateSecs into t1YearSecs
    put tToDaySecs - tBirthDateSecs into tLivedSecs
    put t1stYearSecs - tBirthDateSecs into t1YearSecs
    --------
    if tLivedSecs <= t1YearSecs then
        monthsCount
    else
        yearsCount
    end if
end mouseUp
 
on monthsCount
    set the itemDel to slash
    put item 3 of tToday - item 3 of tBirthDate into tYearsNbr
    ----==================================
    if tYearsNbr >= 0 then
        if tYearsNbr = 0 then put "0" into tAddedMonths
        if tYearsNbr = 1 then put "12" into tAddedMonths
         
        switch
            case item 1 of tToday < item 1 of tBirthDate
                if item 2 of tToday < item 2 of tBirthDate then
                    put tAddedMonths + item 1 of tToday - item 1 of tBirthDate - 1 && "month" into tAge
                else
                    put tAddedMonths + item 1 of tToday - item 1 of tbirthDate && "month" into tAge
                end if
                break
            case item 1 of tToday = item 1 of tBirthDate
                if item 2 of tToday < item 2 of tBirthDate then
                    put tAddedMonths - "1" && "month" into tAge
                end if
                if item 2 of tToday >= item 2 of tBirthDate then
                    put tAddedMonths && "month" into tAge
                end if
                break
            case item 1 of tToday > item 1 of tBirthDate
                if item 2 of tToday < item 2 of tBirthDate then
                    put tAddedMonths + item 1 of tToday - item 1 of tBirthDate -1 && "month" into tAge
                end if
                if item 2 of tToday >= item 2 of tBirthDate then
                    put tAddedMonths + item 1 of tToday - item 1 of tBirthDate && "month" into tAge
                end if
        end switch
    end if
    -----------------
    if word 1 of tAge > 1 then put "months" into word 2 of tAge
    if char 1 of tAge is "-" then put "not born yet" into tAge
    if tAge is "0 month" then put "1st month" into tAge
    if tBirthDate = tToday then put "born to day" into tAge
    --------------
    put tAge into fld "sonAge"
end monthsCount

on yearsCount
    set the itemDelimiter to slash
    put item 3 of tToday - item 3 of tBirthDate into tYearsNbr
    switch
        case item 1 of tToday < item 1 of tBirthDate
            put tYearsNbr - 1 into tAge
            break
        case item 1 of tToday = item 1 of tBirthDate
            if item 2 of tToday < item 2 of tBirthDate then put tYearsNbr -1 into tAge
            if item 2 of tToday = item 2 of tBirthDate then put tYearsNbr into tAge
            if item 2 of tToday > item 2 of tBirthDate then put tYearsNbr into tAge
            break
        case item 1 of tToday > item 1 of tBirthDate
            put tYearsNbr into tAge
    end switch
    --------------------
    if tAge = 1 then
        put tAge && "year" into fld "fldAge"
    else
        put tAge && "years" into fld "fldAge"
    end if
end yearsCount 
 ----------------------------------------









More information about the use-livecode mailing list