use-revolution Digest, Vol 68, Issue 41

Peter Brigham MD pmbrig at gmail.com
Fri May 15 12:48:43 EDT 2009


On May 15, 2009, Kay C Lan wrote:

> Peter,
>
> I gave your function a try and it gave the wrong answer. I had a  
> quick look
> and somewhere along the line it converted 2009 to 1909, so I ended  
> up with a
> negative age. After some of the earlier posts, I guess it has  
> potential for
> a female audience.
>
> Here's my attempt:  <snip>

Hmmm. Never gotten the wrong answer in my use of it. Ahhh, I see.  
There's an error in the function handler

> function getFullYr tDate
> -- works only for people < 100 years old
> set the itemDelimiter to "/"
> put the last item of tDate into yr
> if the number of chars of yr = 2 then
>   if yr >= last item of the short date then  -- wrong comparator
>     put "19" before yr
>   else
>     put "20" before yr
>   end if
> end if
> return yr
> end getFullYr

"if yr >= last item of the short date then" should be "if yr > last  
item of the short date then"

That's what I get for trying to not embarrass myself -- I cleaned up  
working code for my posting to the list, so it looked less  
inefficient, but I didn't test the changes thoroughly.

Actually, I really like Mark Schonewille's version, much more compact.  
I have modified it as below so allow computing the age of any person  
as of an arbitrary date (defaults to today):

function doAge tDate,asOf
    convert tDate to dateItems
    if asOf = empty then
       put the date into asOf
    end if
    convert asOf to dateitems
    put item 1 of asOf - item 1 of tDate - 1 into tAge
    put item 1 of asOf into item 1 of tDate
    convert tDate to seconds
    convert asOf to seconds
    if tDate <= asOf then add 1 to tAge
    return tAge
end doAge

This seems to work well. Anyone find a problem with it?

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig





More information about the use-livecode mailing list