Quantcast
Channel: Sandstorm's Blog (Home of ssUltimate Library)
Viewing all articles
Browse latest Browse all 160

Accurate Age Calculator

$
0
0

I never gave thought to calculating an age of a person in terms of years, months and days. But since I read a thread in Foxite pertaining to such need, then I decided to find out how hard it really is.  And it really is not simple as problems appear in months and days.

Anyway, I always try to think beyond the box meaning I do not limit my tools from within VFP itself so I realize a way to do that easy is via automation using Excel.  Excel has this cool function called DATEDIF to get an accurate difference between years, months and days.  So that is where the experiment went.  And here it is:

Public oAge
oAge = Createobject('EMPTY')
AddProperty(oAge,'Year',0)
AddProperty(oAge,'Month',0)
AddProperty(oAge,'Day',0)

Create Cursor junk (Person c(10),birthdate d, enddate d, nYear I, nMonth I, nDay I)
Insert Into junk Values ('Jun',Date(1971,7,13),Date(),0,0,0)
Insert Into junk Values ('Rolly',Date(1952,9,21),Date(),0,0,0)
Insert Into junk Values ('Whoever',Date(2012,6,11),Date(),0,0,0)
Insert Into junk Values ('Baby 1',Date(2014,8,3),Date(),0,0,0)
Insert Into junk Values ('Baby 2',Date(2014,12,29),Date(),0,0,0)

* Create an Excel file
Local loExcel As excel.Application
loExcel = Createobject('excel.application')
With loExcel
      .Workbooks.Add()
      .DisplayAlerts = .F.
Endwith
Scan
      GetAge(birthdate,enddate,loExcel)
      Replace nYear With oAge.Year, nMonth With oAge.Month, nDay With oAge.Day
ENDSCAN

loExcel.Quit
Browse Normal


******
Function GetAge(dBirth, dEnd, loExcel)
*****
With loExcel
      .Cells(1,1).Value = m.dBirth
      .Cells(2,1).Value = m.dEnd
      .Cells(4,1).Value = '=DATEDIF(A1,A2,"Y")'
      .Cells(5,1).Value = '=DATEDIF(A1,A2,"YM")'
      .Cells(6,1).Value = '=DATEDIF(A1,A2,"MD")'
      oAge.Year = .Cells(4,1).Value
      oAge.Month = .Cells(5,1).Value
      oAge.Day  = .Cells(6,1).Value
Endwith
Return


Quite simple, isn't it?  Cheers!

Viewing all articles
Browse latest Browse all 160

Trending Articles