Calculating age in years as of today for in put date
'Method to calculate age in years for the given date
Function GetAge(ByVal dt As Date) As Integer
Dim dtVal As Date
Dim retval As Integer
If IsDate(dt) Then
dtVal = Date.Parse(dt)
retval = DateTime.Now.Year - dtVal.Year
'subtract a year if date is before current year date
If DateTime.Now.Month < dtVal.Month Or (DateTime.Now.Month = dtVal.Month And DateTime.Now.Day < dtVal.Day) Then
retval = retval - 1
End If
End If
Return retval
End Function
Calling getAge() in button click
'Calling the method
Private Sub btnAge_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MessageBox.Show(GetAge("10/2/2005"))
End Sub