VB.NET - Calculating age in years

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
Share |

 Cant find the page you are looking for?
 Help us to improve by adding the content that you are looking for.
 Leave a feedback
 We look forward to hear your comments and feedback.