|
-
Oct 7th, 2010, 02:16 AM
#1
Thread Starter
Frenzied Member
Calculate Exact Age
I need to calculate the age in Years,Months and Days for which i am using the following code, but it give inappropriate result
vb Code:
Dim d As Date
If Date.TryParse(Me.DOB.Text, d) Then
Dim ts As TimeSpan = Today - d
Dim y As Double
Dim m As Double
Dim ds As Double = ts.TotalDays
y = Math.Floor(ds / 365)
ds -= y * 365
m = Math.Floor(ds / 31)
ds -= m * 31
Me.TxtYears.Text = cstr(y)
Me.TxtMonths.Text=Cstr(m)
Me.TxtDays.Text = Cstr(ds)
End If
I also need to do the reverse calculation i.e get the DOB from the Age using the txtYears, txtMonths and TxtDays
Plz Help Me
-
Oct 7th, 2010, 08:29 AM
#2
Re: Calculate Exact Age
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
-
Oct 8th, 2010, 12:56 AM
#3
Thread Starter
Frenzied Member
Re: Calculate Exact Age
Thanks, but i need months also
-
Oct 8th, 2010, 06:55 AM
#4
Re: Calculate Exact Age
 Originally Posted by aashish_9601
Thanks, but i need months also
What is a month?
Try this modified version
Code:
Public Function AgeCalc(ByVal DatePast As DateTime, _
Optional ByVal DateFuture As DateTime = Nothing, _
Optional ByVal returnMosDay As Boolean = False) As String
'calculate age using two dates.
'the age returned will be years and days or years months 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)
'or
'Debug.WriteLine(ageSTR(0)) 'years
'Debug.WriteLine(ageSTR(1)) 'months
'Debug.WriteLine(ageSTR(2)) 'days
'Debug.WriteLine(ageSTR(3)) '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
Dim mos As Integer = 0
If returnMosDay Then 'return years mos days
df = df.Date
Dim na As DateTime = nxtAnniv.Date 'get anniversary date
Dim la As DateTime = nxtAnniv.Date 'set trailing anniversary
Do While na < df
na = na.AddMonths(1) 'add one month
If na < df Then 'less than future date
Dim td As Integer = (na - la).Days
If td <= days Then 'enough days?
days -= td 'subtract days
mos += 1 'increment monts
la = na 'set trailing anniversary
Else
Exit Do
End If
Else
Exit Do
End If
Loop
retval = years.ToString & "|" & mos.ToString & "|" & days.ToString 'create return value
Else 'return years and days
retval = years.ToString & "|" & days.ToString 'create return value
stpw.Stop() 'how long did it take?
End If
retval &= "|" & stpw.ElapsedTicks.ToString
Return retval
End Function
Last edited by dbasnett; Oct 8th, 2010 at 07:14 AM.
-
Oct 8th, 2010, 07:17 AM
#5
VB6 Library
If I helped you then please help me and rate my post!
If you solved your problem, then please mark the post resolved
-
Oct 8th, 2010, 08:08 AM
#6
Re: Calculate Exact Age
 Originally Posted by MarMan
Use DateDiff.
In the code I provided I tried to preserve days also. If you just wanted months the code could have been simpler. Little about DateTime is straight forward.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|