Calculate Age - Visual Basic .NET

Tags: VB.NET, VB 2008, VB 2010, VB 2012, VB 2013

For This Project you need 4 TextBoxes and 1 Button

Calculate Age-
TextBox1 will be used to enter the month of birth
TextBox2 will be used to enter the day of birth
TextBox3 will be used to enter the year of birth
TextBox4 will be used to show the calculated age

Button1 will be used to calculate age after the user enters DOB

Below is the code for Button1.Click

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
 Handles Button1.Click
        Dim a As String
        Dim b As String
        Dim c As String
        a = TextBox3.Text
        b = TextBox1.Text
        c = TextBox2.Text
        Dim DOB As New DateTime(a, b, c)
        Dim Years As Integer = DateDiff(DateInterval.Year, DOB, Now) - 1
        Dim Months As Integer = DateDiff(DateInterval.Month, DOB, Now) Mod 12
        Dim days As Integer = DateDiff(DateInterval.Day, DOB, Now) Mod 30 - 10
        TextBox4.Text = Years & " Years, " & Months & " Months "
End Sub

Share This

Home | About | Contact | Privacy Policy

Copyright 2018 - All Rights Reserved.