Age calculation script

Jim Ault JimAultWins at yahoo.com
Sat Sep 9 13:36:44 EDT 2006


On 9/9/06 10:21 AM, "Roger.E.Eller at sealedair.com"
<Roger.E.Eller at sealedair.com> wrote:

> On 9/9/06 12:03 PM, "Jim Ault" <JimAultWins at yahoo.com> wrote:
>> 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.
> 
> It appears that any birth year before 1970 results in an incorrect
> calculation.  Entering 5/18/65, the result is:  days old = 54750   years old =
> 149   exact years old = 149.945205   next
> Bday = 20 days from now
> 
Hmmmm, my version seems to work fine.  Maybe I did a typo or something in
building the message to the list.
--------------- using quotes around the date
calcbackdate "5/18/65"
days old = 15089   years old = 41
exact years old = 41.312329   next Bday = 251 days from now

------------------- using quotes & a leading space
calcbackdate " 5/18/65"
days old = 114   years old = 0
exact years old = 0.312329   next Bday = 251 days from now


------------------- using NO quotes
calcbackdate 5/18/65
days old = 13401   years old = 36
exact years old = 36.690411   next Bday = 113 days from now

----------------------------------------------------------------------------
here is the fresh version of the script I am using.
See if it makes a difference
 
--start copy here
on calcbackdate  birthdate
   put word 1 of birthdate into birthdate
  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
  put the short date into noww
  convert noww to seconds
  put 24*60*60 into hr24
  repeat with x = 1 to 36500 --99+ years
    put noww - (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 calcbackdate

--end copy



Jim Ault
Las Vegas
















More information about the Use-livecode mailing list