I dug this up. It does years and days
Code:Public Function AgeCalc(ByVal DatePast As DateTime, _ Optional ByVal DateFuture As DateTime = Nothing) As String 'calculate age using two dates. the age will be years and days 'USE: 'Dim ageSTR() As String = AgeCalc(#2/29/1008#, #2/28/2009#).Split("|"c) 'Debug.WriteLine(ageSTR(0)) 'years 'Debug.WriteLine(ageSTR(1)) 'days 'Debug.WriteLine(ageSTR(2)) 'how long calculation took (in ticks) ' 'note - the wonderful thing about DateSerial is that it takes care of 'Leap Birthdays AKA leaplings 'a leaplings b-day is 3/1 for non-leap years Dim dp, df, nxtAnniv As DateTime, retval As String, ts As New TimeSpan Dim years, days As Integer, stpw As New Stopwatch stpw.Start() 'provide metric If DateFuture = Nothing Then DateFuture = DateTime.Now 'use now for future date If DatePast > DateFuture Then 'make sure the past is the past dp = DateFuture df = DatePast Else dp = DatePast df = DateFuture End If years = df.Year - dp.Year 'calculate years 'create next "birthday" 'nxtAnniv = New System.DateTime(df.Year, dp.Month, dp.Day) 'does not take care of leap b-day nxtAnniv = DateSerial(df.Year, dp.Month, dp.Day) 'takes care of leap b-day If nxtAnniv > df Then 'is the next "birthday" > future date years -= 1 'yes, adjust year nxtAnniv = DateSerial(df.Year - 1, dp.Month, dp.Day) 're-create so it is before future End If ts = df - nxtAnniv 'will give days days = ts.Days retval = years.ToString & "|" & days.ToString 'create return value stpw.Stop() 'how long did it take? retval &= "|" & stpw.ElapsedTicks.ToString Return retval End Function




Reply With Quote