Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Accurately calculating a persons age
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 8 SP1
OS:
Windows XP SP2
Network:
Windows 2003 Server
Database:
MS SQL Server
Miscellaneous
Thread ID:
00953701
Message ID:
00954178
Views:
17
>The system I am writing needs to validate people applying for an account as being 18 years old or older. Anyone under 18 should not be allowed an account. It sounds simple, but one of my versions that included detection of leap years got very complicated, hence my question here.
>
>Simon


Simon, try this & see if it works for You (the function is not very well tested):
? CalcAge(DATE(1965, 4, 26), DATE(2004,4,27))

FUNCTION CalcAge(b_day, to_date)
  LOCAL fmnts, smnts
  fmnts = YEAR(b_day)  *12+MONTH(b_day)

  IF MONTH(to_date) == MONTH(b_day) .AND.; && If The day is smaller then day of birthday the person has not complete full year
     DAY(to_date)   <  DAY(b_day)
     smnts = YEAR(to_date)*12+(MONTH(to_date)-1)
  ELSE
     smnts = YEAR(to_date)*12+MONTH(to_date)
  ENDIF

RETURN INT((smnts-fmnts)/12)
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