calculate age in vb.net

To calculate age of a person using VB.Net from date of birth you can use DateDiff builtin function here a user function to calculate age in years.

Public Function Calculate_Age(ByVal Birth_Date As Object) As Long

        If Not IsDate(Birth_Date) Then Return 0

        Dim dtTemp As Date
        Try
            dtTemp = CDate(Birth_Date)
        Catch ex As Exception
            Return 0
        End Try

        If dtTemp > Now Then Return 0

        Try
            Return DateDiff("yyyy", dtTemp, Date.Now) + (DateSerial(Year(Date.Now), Month(dtTemp), dtTemp.Day) >= Date.Now)
        Catch ex As Exception
            Return 0
        End Try

End Function




Add Comments

Name: *
Email: *
URL:
Comments: *