Age calculation script

Jim Ault JimAultWins at yahoo.com
Sat Sep 9 12:03:59 EDT 2006


Here is my version using the approach of 'dateitems'
This rendition *should* do things correctly and accommodate leap years.  It
will also calculate days till next birthday.

To run, type into message box  => calcAge "7/4/59"
then hit the enterkey

on calcAge  birthdate, asOf
  if birthdate contains "2/29" then
    replace "2/29" with "2/28" in birthdate
    put true into leapBirthday --if you need this
  end if
  convert birthdate to dateitems
  put item 1 to 3 of birthdate into birthdate
  if asOf is empty then put the short date into asOf
  convert asOf to seconds
  put 24*60*60 into hr24
  repeat with x = 1 to (365*150)
    put asOf - (x*hr24) into past
    convert past to dateitems
    if past contains ",2,29," then add 1 to leapDays
    if past contains birthdate then exit repeat --we are done
  end repeat
  put (x - leapDays)/365 into ageInYearsDecimal
  put (x - leapDays) div 365 into ageInYears
  put x into ageInDays
  put ((ageInYears+1) -ageInYearsDecimal)*365 into daysTillNext
  --did not test for upcoming leap year
  put "days old = " & ageInDays && "  years old = "& ageInYears&cr \
      & "exact years old = "& ageInYearsDecimal&& "  next Bday = "&
daysTillNext \
      & " days from now"
end calcAge


Jim Ault
Las Vegas

On 9/8/06 8:10 PM, "Peter Brigham" <pmbrig at comcast.net> wrote:

> 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.
> ********
> ****
> _______________________________________________
> 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