Age from DOB

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
Philhold
Posts: 64
Joined: Thu Mar 05, 2009 3:39 pm

Age from DOB

Post by Philhold » Tue Oct 27, 2009 2:30 pm

Simple age from DOB function converted from PHP. I'm just posting this here in case it is a useful starter for anyone. Suggestions for improvement very welcome.

Code: Select all

function agefromDOB varDOB
   -- Validate var DOB is in DD/MM/YYYY format 
   if matchtext(varDOB, "((0?[1-9]|[12][0-9]|3[01])[/](0?[1-9]|1[012])[/](19|20)[0-9]{2})") then
    split varDOB by "/"
   end if
    
    put the date into varToday
    split varToday by "/"
    
    
    put (varToday[3]+2000) - varDOB[3] into varYrDiff
    put varToday[2] - varDOB[2] into varMonDiff
    put varToday[1] - varDOB[1] into varDayDiff 
    
    if varMonDiff < 0 then put varYrDiff -1 into varYrDiff
    else  if varMonDiff = 0 then
        if varDayDiff < 0 then
            put (varYrDiff-1) into varYrDiff
        end if
    end if
    return varYrDiff   
    
end agefromDOB varDOB 


on mouseUp pMouseBtnNo
 put field "DOB" into varDOB
    get agefromDOB(varDOB)  
  put it into field "Age"
    
end mouseUp
MM/DD/YYYY version

Code: Select all

function agefromDOB varDOB
   -- Validate var DOB is in MM/DD/YYYY format 
   if matchtext(varDOB, "(((0?[1-9]|1[012])[/]0?[1-9]|[12][0-9]|3[01])[/](19|20)[0-9]{2})") then
    split varDOB by "/"
   end if
    
    put the date into varToday
    split varToday by "/"
    
    
    put (varToday[3]+2000) - varDOB[3] into varYrDiff
    put varToday[1] - varDOB[1] into varMonDiff
    put varToday[2] - varDOB[2] into varDayDiff 
    
    if varMonDiff < 0 then put varYrDiff -1 into varYrDiff
    else  if varMonDiff = 0 then
        if varDayDiff < 0 then
            put (varYrDiff-1) into varYrDiff
        end if
    end if
    return varYrDiff   
    
end agefromDOB varDOB 


on mouseUp pMouseBtnNo
 put field "DOB" into varDOB
    get agefromDOB(varDOB)  
  put it into field "Age"
    
end mouseUp

Post Reply