Age & Anniversary Calculations
Peter M. Brigham, MD
pmbrig at gmail.com
Mon May 21 16:01:57 EDT 2012
Sorry, I copied and pasted an older version of the handler. Try this:
function doAge tDate,asOf
-- returns the age given birthdate = <tDate>
-- as of the date <asOf> (if empty, assumes today)
if tDate = empty then return empty
if asOf = empty then
put the date into asOf
end if
set the itemdelimiter to "/"
put item -1 of the short date into nowYr
set the centurycutoff to nowYr+1
set the itemdelimiter to comma
convert tDate to dateItems
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
Don't know if there is another problem with it, but I have been using this version for some time with no errors cropping up that I have noticed.
-- Peter
Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig
On May 21, 2012, at 3:02 PM, Bob Sneidar wrote:
> Wasn't there a recent post that indicated this would not work? Anything before a certain date (I forget what it was) would be a day off?
>
> Bob
>
>
> On May 21, 2012, at 9:38 AM, Peter M. Brigham, MD wrote:
>
>> Here's what I use for #1:
>>
>> function doAge tDate,asOf
>> -- return the age given birthdate = <tDate>
>> -- as of the date <asOf> (if empty, assumes today)
>> 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
>>
>> -- Peter
>>
>> Peter M. Brigham
>> pmbrig at gmail.com
>> http://home.comcast.net/~pmbrig
>>
>>
>> On May 21, 2012, at 3:02 AM, Igor de Oliveira Couto wrote:
>>
>>> Dear LC Folks,
>>>
>>> What is the 'best' way to perform these 2 calculations with LiveCode?
>>>
>>> 1) Calculate a person's age, given a birthdate:
>>>
>>> /*
>>> Calculates the number of birthdays from a given date, up to and including *today*.
>>> @param pBirthDate the birthdate
>>> @return a positive integer representing the number of birthdays from pBirthDate to today.
>>> */
>>> function currentAge pBirthDate
>>> ...?
>>> end currentAge
>>>
>>>
>>> 2) Calculate whether an *anniversary* falls within a time frame - ie., within a week, within 2 weeks, within a month of today:
>>>
>>> /*
>>> Determines whether an anniversary date is close to a 'reference date'.
>>> @param pAnniversaryDate the anniversary date
>>> @param pReferenceDate the date that is supposed to be 'close' - or not - to the anniversary date
>>> @pTimeFrame a time period, in seconds, that determines what 'close' is: it can be 1 day, 7 days, 1 month, etc.
>>> @return a boolean: if the anniversary date is sufficiently 'close' (as specified by pTimeFrame) to the reference date,
>>> then it returns TRUE, otherwise it returns FALSE.
>>> */
>>> function isAnniversaryClose pAnniversaryDate, pReferenceDate, pTimeFrame
>>> ...?
>>> end isAnniversaryClose
>>>
>>> I'm not being lazy, but the algorithms I came up with look so 'convoluted' that I *know* there must be an easier way. I know that usually in every language there is a 'standard' or 'best-practice' way to perform these standard date calculations, so rather than re-inventing the wheel, I thought I'd run it by you, Masters, and be humbled by your experience! :-)
>>>
>>> Many thanks in advance,
>>>
>>> --
>>> Igor Couto
>>> Sydney, Australia
>>>
>>>
>>> _______________________________________________
>>> use-livecode mailing list
>>> use-livecode at lists.runrev.com
>>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>>> http://lists.runrev.com/mailman/listinfo/use-livecode
>>
>>
>> _______________________________________________
>> use-livecode mailing list
>> use-livecode at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
>> http://lists.runrev.com/mailman/listinfo/use-livecode
>
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
More information about the use-livecode
mailing list