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:
00115979
Views:
12
Paul,

Thank you (or merci')! I'll check this one out, too. For now, though, my time is extremely limited since I'm leaving tomorrow night to drive from Maryland to San Diego with my wife and kids. (Why is my rear end starting to get numb ALREADY???)

This is a variation of what Ernie sent in from Cetin. Thank you!!!

Thanks to everyone for their replies! This has been a great experience. Here's the code I'll be using:

- Gerry

FUNCTION AgeInYMD
LPARAMETERS tcDetail, dBirthDate, dTargetDate, aYMD

** Not enough parameters - adios! **
IF PARAMETERS() < 2
RETURN {}
ENDIF
** No target date? Assume today's date**
IF EMPTY(dTargetDate)
dTargetDate = DATE()
ENDIF

nYears = YEAR(dTargetDate) - YEAR(dBirthDate)
IF GOMONTH(dBirthDate,nYears * 12) > dTargetDate
nYears = nYears - 1
ENDIF

dBirthDate = GOMONTH(dBirthDate,nYears*12)

nMonths = 0
DO WHILE MONTH(dBirthDate) # MONTH(dTargetDate)
dBirthDate = GOMONTH(dBirthDate,1)
nMonths = nMonths + 1
ENDDO

IF DAY(dBirthDate) > DAY(dTargetDate)
nMonths = nMonths - 1
dBirthDate = GOMONTH(dBirthDate,-1)
ENDIF

nDays = dTargetDate - dBirthDate

** put info in array **
IF TYPE("aYMD") # "U"
DIMENSION aYMD[3]
aYMD[1] = nYears
aYMD[2] = nMonths
aYMD[3] = nDays
ENDIF

DO CASE && number returned
CASE tcDetail == "Y" && 44 = 44 years old
nAgeNum = INT(nYears)
CASE tcDetail == "YM" && 4401 = 44 yrs and 1 month
nAgeNum = INT(nYears * 10^2 + nMonths)
CASE tcDetail == "YMD" && 440111 = 44 yrs, 1 month, and 11 days
nAgeNum = INT(nYears * 10^4 + nMonths * 10^2 + nDays)
ENDCASE
RETURN PADL(nAgeNum,6,"0")

Now you'll just have to parse the result appropriately.
(I modified the code a bit - to return an integer, to add comments, return a fixed-length result, and to do an exact match on tcDetail.)

>Just for the fun of it, here's another one:
>
>
*-- Calculates the difference between two dates
>*   The two dates can be in any order
>lparameters tdDate1, tdDate2
>local ldDate1, ldDate2
>
>*-- The following test sets ldDate1 < ldDate2
>if tdDate1 < tdDate2
>	ldDate1 = tdDate1
>	ldDate2 = tdDate2
>else
>	ldDate1 = tdDate2
>	ldDate2 = tdDate1
>endif
>
>*-- Calculate the years
>*-- Example: 1967/01/27, 1998/03/28
>*   int((19980328 - 19670127) / 10000) =
>*   = int(310201 / 10000) = int(31.0201) = 31
>lnYears = int((val(dtos(ldDate2)) - val(dtos(ldDate1))) / 10000)
>
>*-- Calculate the months
>*-- This can be done regardless of years...
>lnMonths = month(ldDate2) - month(ldDate1)
>if day(ldDate2) < day(ldDate1)
>	lnMonths = lnMonths - 1
>endif
>if lnMonths < 0
>	lnMonths = 12 + lnMonths
>endif
>
>*-- Calculate the days
>ldDate1 = gomonth(ldDate1, lnYears * 12 + lnMonths)
>lnDays = ldDate2 - ldDate1
>
>?lnYears
>?lnMonths
>?lnDays
>
>Vlad
** Gerry White / Hagerstown, MD, USA --
** Developing since 1986 in Foxpro, VFP, Oracle, and Java.
** http://GerrysPlace.com
Previous
Reply
Map
View

Click here to load this message in the networking platform