Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
What is best way to calculate age
Message
From
20/09/2006 18:03:53
 
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:
01155763
Views:
25
>Am I right to go back to the person's birthday knowing his age today?
>=MESSAGEBOX(GOMONTH(GOMONTH(DATE(),-18*12),-6)- 22)
You can't go back using a different calculation and expect to always get the same result. Consider someone born on Feb. 29, 2004 (a leap year). How old is that person on Feb. 28, 2005? I think most people would say the person is one year old exactly, which is what my age calculator says, too. Now, how old is that person one day later, on Mar. 01, 2005? Again, I think most people would agree the person is one year and one day old, which is what my age calculator also says.

If you work backwards from those results using the GOMONTH formula you suggest, however, then you will end up with Feb. 28, 2004 in the first case and Feb. 29, 2004 in the second case. Try the code below and see for yourself:
lox = NEWOBJECT( "AgeCalculator", "age.prg")
CLEAR
ldDate1 = {^2004-02-29}
ldDate2 = {^2005-02-28}
lox.CalculateAge( ldDate1, ldDate2)
?DTOC( ldDate1) + " to " + DTOC( ldDate2) + " = " + ;
   TRANSFORM( lox.nYears) + " years " + ;
   TRANSFORM( lox.nMonths) + " months " + ;
   TRANSFORM( lox.nDays) + " days"
?"Reverse calculation = " + DTOC( GOMONTH(GOMONTH(ldDate2, -lox.nYears* 12) , -lox.nMonths) - lox.NDAYS)
* Result:
* 02/29/2004 to 02/28/2005 = 1 years 0 months 0 days
* Reverse calculation = 02/28/2004

* and 

ldDate1 = {^2004-02-29}
ldDate2 = {^2005-03-01}
lox.CalculateAge( ldDate1, ldDate2)
?DTOC( ldDate1) + " to " + DTOC( ldDate2) + " = " + ;
   TRANSFORM( lox.nYears) + " years " + ;
   TRANSFORM( lox.nMonths) + " months " + ;
   TRANSFORM( lox.nDays) + " days"
?"Reverse calculation = " + DTOC( GOMONTH(GOMONTH(ldDate2, -lox.nYears* 12) , -lox.nMonths) - lox.NDAYS)
* Result:
* 02/29/2004 to 03/01/2005 = 1 years 0 months 1 days
* Reverse calculation = 02/29/2004
To go backwards and always arrive at the same starting point would require a more precise calculation than GOMONTH() alone is going to give you.

Whenever you work with date arithmetic you have make certain decisions about what constitutes a "month" and even a "year". The decisions I made for my age calculator class are documented in the readme file.
Rick Borup, MCSD

recursion (rE-kur'-shun) n.
  see recursion.
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform