Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
What is best way to calculate age
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01155605
Message ID:
01155686
Views:
30
>>I never need to calculate ages, but here what I invent :-)) (I hate endless loops)
>>
>>ldBegin = DATE(1965, 4, 26)
>>ldEnd   = DATE()
>>STORE 0 TO lnYear, lnMonhts, lnDays
>>DO WHILE .t.
>>   DO CASE
>>      CASE GOMONTH(ldBegin,12) <= ldEnd
>>           lnYear = lnYear + 1
>>           ldBegin = GOMONTH(ldBegin, 12)
>>      CASE GOMONTH(ldBegin,1) <= ldEnd
>>           lnMonhts = lnMonhts + 1
>>           ldBegin  = GOMONTH(ldBegin, 1)
>>      CASE ldBegin            < ldEnd
>>           lnDays  = lnDays  + 1
>>           ldBegin = ldBegin + 1
>>   OTHERWISE
>>           EXIT
>>   ENDCASE
>>ENDDO
>>? lnYear, lnMonhts, lndays
>>
>>Of couse there is no checking for valid date period and no error handling at all. Also not well tested :-)))
>
>The first very serious problem this code has is lnMonths is spelled wrongly <g>
>
>Add a back date calculation to your code to see, if the dates match.


Too much beer :o))))

Here the corrected code :-)
ldBegin = DATE(2006, 4, 26)
ldEnd   = DATE()
STORE 0 TO lnYear, lnMonths, lnDays
DO WHILE .t.
   DO CASE
      CASE GOMONTH(ldBegin,12) <= ldEnd
           lnYear = lnYear + 1
           ldBegin = GOMONTH(ldBegin, 12)
      CASE GOMONTH(ldBegin,1) <= ldEnd
           lnMonths = lnMonths + 1
           ldBegin  = GOMONTH(ldBegin, 1)
      CASE ldBegin            < ldEnd
           lnDays  = lnDays  + 1
           ldBegin = ldBegin + 1 
   OTHERWISE
           EXIT 
   ENDCASE
ENDDO
? lnYear, lnMonths, lndays
? GOMONTH(DATE(),(-lnYear*12)-lnMonths)-lndays+1
Against Stupidity the Gods themselves Contend in Vain - Johann Christoph Friedrich von Schiller
The only thing normal about database guys is their tables.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform