Age calculation script

Peter Brigham pmbrig at comcast.net
Fri Sep 8 23:10:35 EDT 2006


Weighing in a little late here (I get the digest version, so there's  
some lag time)... Here's what I use in a patient notes stack in my  
practice. I tried using routines based on seconds but always ran into  
errors when the today was close to the birthday. The leap year thing  
is a bear to get exactly right. I finally went to something that  
looks at the dates:

function doAge bd,asOf
   if bd = "" then
     return ""
   end if
   set the itemDelimiter to "/"
   put the last item of bd into yr
   if the number of chars of yr = 2 then
     if last item of bd >= last item of the short date then
       put "19" before item 3 of bd
     else
       put "20" before item 3 of bd
     end if
     put item 3 of bd into actualBdYr
   else
     put yr into actualBdYr
   end if
   if asOf = "" then
       put the short date into asOf
   end if
   put the last item of asOf into yr
   if the number of chars of yr = 2 then
     if last item of asOf > last item of the short date then
       put "19" before item 3 of asOf
     else
       put "20" before item 3 of asOf
     end if
     put item 3 of asOf into actualNowYr
   else
     put yr into actualNowYr
   end if
   set the itemDelimiter to ","
   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 nM
   if bMo > nMo then subtract 1 from nY
   put nYr - bYr into age
   return age
end doAge

************************

On Fri, 8 Sep 2006 14:44:38 +0100 Mark Smith <mark at hidden> wrote:

****
Re: Age calculation script

replacing
put 60 * 60 * 24 * 365 into secsPerYear

with
   put (60 * 60 * 24 * 365) + (60 * 60 * 6)  into secsPerYear

ie. adding a quarter of a days seconds to sescPerYear


seems to fix it, at least for age < 30ish.

Best,

Mark

On 8 Sep 2006, at 14:36, Mark Smith wrote:

********
  Actually, the other way it is likely to be wrong is when today's
date is very close to the persons birthday - eg if a persons age is
really 16 yrs 11 months, and their birthday is a couple of days
from now, it may give 17 yrs 0 months.
********
****



More information about the use-livecode mailing list