Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Function to determine age, with part years?
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
Visual FoxPro
Application:
Desktop
Miscellaneous
Thread ID:
01491452
Message ID:
01491596
Views:
73
1) The part of the years, I do use it in production. The fractional part, I have coded yesterday :-)

2) This condition is still not his birthday.
GOMONTH(tdBirthDate, 12 * lnYears) > tdToday
These are the days until or last your birthday.
tdToday - GOMONTH(tdBirthDate, 12 * lnYears)
And if the year is a leap year, divided by 366 these days, but by 365
IIF(YEAR(tdToday) % 4 = 0 AND (YEAR(tdToday) % 100 # 0) OR (YEAR(tdToday) % 400 = 0), 366, 365)
>Hi Luis,
>
>Couple of questions:
>
>1) has this been tested in production and found accurate, always
>2) could you document the logic a bit so I can follow what you are doing (particularly the fractions part).
>
>Thanks,
>Albert
>
>>>Hi all,
>>>
>>>Does anyone have a bulletproof function to determine "age" between 2 dates - that takes into account leap years and also is able to report back part years (as a decimal part of the number) e.g. 21.3671 for 21 years and 134 days into the next year.
>>>
>>>I wrote one years ago that loops through the years between the start date and end date adjusting for leap years and it works for the most part. It slows the report down somewhat as it is called a few hundred times and has to run this loop each time.
>>>
>>>The customer wants it to be "accurate" for the decimal part so that if it was 134 days into the new years and that year is not a leap year, it would return .3671 for the decimal portion (134/365) and if it *was* a leap year, it would return .3661 (134/365). Not sure their "logic" on that last bit is "correct" but that's what they want.
>>>
>>>So in short, a faster function than a loop would be good as long as it can return days as a decimal fraction.
>>>
>>>thanks,
>>>Albert Gostick
>>
>>Works fine with leap years.
>>
>>
>>? Age(DATE(2000,12,2))
>>
>>FUNCTION Age(tdBirthDate, tdToday)
>>  LOCAL lnYears, lnFraction
>>  IF EMPTY(tdToday)
>>    tdToday = DATE()
>>  ENDIF
>>  lnYears = YEAR(tdToday) - YEAR(tdBirthDate)
>>  IF GOMONTH(tdBirthDate, 12 * lnYears) > tdToday
>>    lnYears = lnYears - 1
>>  ENDIF
>>  lnFraction = (tdToday - GOMONTH(tdBirthDate, 12 * lnYears)) / ;
>>    IIF(YEAR(tdToday)%4 = 0 AND (YEAR(tdToday)%100 # 0) OR (YEAR(tdToday)%400 = 0), 366, 365)
>>  RETURN lnYears + lnFraction
>>ENDFUNC
>>
Luis María Guayán
Tucumán, Argentina
________________________________
SysOp de www.PortalFox.com
Nada corre como un zorro
________________________________
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform