Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Age to Years,Month,Day - Problem With
Message
From
11/09/2002 06:18:16
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00699014
Message ID:
00699234
Views:
23
>The code below most of the time seems to return the correct value. It is supposed to return number of years, Months and days given the date of birth and the current date. But today, I found out, that if I pass it the dob as 09/30/1958, I get 43 Years, -1 Month and 376 days. I can't see this as correct. If anybody can help spot my problem I would greatly appreciate it.
>
>
>*************
>* AgeInYMD
>* Parameters :
>* date DateOfBirth,
>* date TargetDateToCalculateAge (If not passed date() assumed),
>*
>* return : Character - Age in YYYYMMDD format
>*************
>
>LPARAMETERS dBirthDate, dTargetDate
>IF parameters() < 1
>    RETURN {}
>ENDIF
>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
>
>
>lcReturn = [|]+ALLTRIM(STR(nYears))+[|]+ALLTRIM(STR(nMonths))+[|]+ALLTRIM(STR(nDays))+[|]
>
>RETURN (lcReturn)
>
lparameters tdDate1, tdDate2
local lnYears, lnMonths, lnDays, ldTemp, ldCalcDate
IF parameters() < 1
    RETURN replicate('0',8)
ENDIF
tdDate2 = IIF(empty(tdDate2),date(),tdDate2)

if tdDate2 < tdDate1
	ldTemp = tdDate1
	tdDate1 = tdDate2
	tdDate2 = ldTemp
endif

lnMonths = floor((tdDate2-tdDate1)/31)
ldCalcDate = gomonth( tdDate1, lnMonths )
if ldCalcDate > tdDate2
	ldCalcDate = tdDate1
	lnMonths = lnMonths - 1
endif
do while gomonth( ldCalcDate, 1 ) <= tdDate2
  ldCalcDate = gomonth( ldCalcDate, 1 )  
  lnMonths = lnMonths + 1
enddo
lnYears = int( lnMonths / 12 )
lnMonths = lnMonths - ( lnYears * 12 )
lnDays   = tdDate2 - ldCalcDate
RETURN trans(int(lnYears * 10^4 + lnMonths * 10^2 + lnDays),'@R 9999/99/99')
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform