Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Calc age in years+months+days - help, please
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00115698
Message ID:
00115752
Views:
17
The two other suggestiong I saw are certainly shorter than mine but if you feed them 11/14/1968 in the first case, and 12/31/1968 in teh second the months and days are off respectively. This is long, but seems to be accurate - maybe it can be cleaned up a bit:
PARAMETER dbdate
LOCAL lnyears, lnmonths, lndays, ldtemp, lbday

** Calculate how many years
** Based on if they have had a birthday yet this year
DO CASE

CASE month(date())>month(dbdate)
lnyears=year(date())-year(dbdate())

CASE month(date()) lnyears=year(date())-year(dbdate)-1

CASE month(date())=month(dbdate)
**So now we have to figure out
** if the person had a birthday
** this year yet
IF day(date())>=day(bdate)
lnyears=year(date())-year(dbdate())
lbday=.t. && They had a birthday this year
ELSE
lnyears=year(date())-year(dbdate())-1

ENDIF
ENDCASE

** Now calculate the month
** based on counting from the last birthday
** This one is kludgy but...
ldtemp=gomonth(dbdate,12)
IF month(ldtemp)=month(date())
** If it's their birth month now
** it will be either 11 or 0 months
** in the months value
IF lbday && Calculated above
lnmonths=0
ELSE
lnmonths=11
ENDIF
ELSE
**If it's not their birth month it's a value from 1 to 10

FOR i = 1 to 10
IF month(gomonth(dbdate,i))=month(date())

** You are down to the month now all that
** is left is days
IF day(gomonth(dbdate,i))<=day(date())
lnmonths=i
lndays=day(date())-day(gomonth(dbdate,i)
ELSE
lnmonths=i-1 && Not a full month
** Calculate the days
** Get the last day of the prior month
ldtemp=gomonth(dbdate,i)-day(gomonth(dbdate,i))


lndays=day(date())+
(day(ldtemp)-day(gomonth(dbdate,i-1)))

ENDIF
ENDIF
ENDFOR
ENDIF

@ 1,1 say alltrim(str(lnyears)) + " years"
@ 2,1 say alltrim(str(lnmonths)) + " months"
@ 3,1 say alltrim(str(lndays ))+ " days"
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform