Completely Free .NET word API

Resources » Code Snippets » ASP.NET WebForms

Code to Calculate Exact Age programmatically using vb.NET

Updated: Category: ASP.NET WebForms
Author: Member Level: GoldPoints: 8

Code to Calculate Exact Age in vb.NET


This code is use to calculate your exact Age. You have enter your Date Of Birth in TextBox Control in the form of


Add or copy & paste below code in HTML page

<table cellpadding="0" cellspacing="0" border="1" align="center">
<asp:Label ID="Lblresponse" runat="server" Text="Enter Date of Birth"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="MM/DD/YY"></asp:Label>
<td align="center">
<asp:Button ID="Button1" runat="server" Text="Calculate Age" />
<asp:Label ID="LblStatus" runat="server"></asp:Label>

Declare Variable to hold the Date of Birth

Dim DateOfBirth As DateTime

Add below code on Calculate Age button click Event

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click


If TextBox1.Text = "" Then
' if TextBox is Blank then do nothing

' Pass textbox value to DateofBirth variable

DateOfBirth = Trim(TextBox1.Text.ToString())
Dim DOB As DateTime = DateOfBirth
Dim Years As Integer = 0
Dim Month As Integer = 0
Dim Days As Integer = 0
Dim StrAge As String = Nothing
' Check if the DOB is less than current date
If DOB < DateTime.Now Then
' Calculate Difference between current date and DOB
Dim dateDiff As TimeSpan = DateTime.Now - DOB
Dim age As New DateTime(dateDiff.Ticks)
Years = age.Year - 1
Month = age.Month - 1
Days = age.Day - 1
StrAge = ((Years.ToString() & " Years ") + Month.ToString() & " Months ") + Days.ToString() & " Days "
StrAge = "Entered Date Of Birth is less then Current Date"
End If
' Print the Exact Age
LblStatus.Text = "Youe Exact Age is : " & StrAge
End If
Catch ex As Exception
End Try
End Sub

Nilesh Jadhav

Did you like this resource? Share it with your friends and show your love!

Responses to "Code to Calculate Exact Age programmatically using vb.NET"
Author: Deepthi  17 Dec 2009   Member Level: Gold   Points : 1

Can u post the code in C# also.As i am unaware of VB.NET.


Author: Nilesh Prabhakarrao Jadhav  17 Dec 2009   Member Level: Gold   Points : 2
Hi Deepthi,

As per your requirment below is the C# code

try {
if (string.IsNullOrEmpty(TextBox1.Text)) {
else {
DateOfBirth = Strings.Trim(TextBox1.Text.ToString());
DateTime DOB = DateOfBirth;
int Years = 0;
int Month = 0;
int Days = 0;
string StrAge = null;
if (DOB < DateTime.Now) {
TimeSpan dateDiff = DateTime.Now - DOB;
DateTime age = new DateTime(dateDiff.Ticks);
Years = age.Year - 1;
Month = age.Month - 1;
Days = age.Day - 1;
StrAge = ((Years.ToString() + " Years ") + Month.ToString() + " Months ") + Days.ToString() + " Days ";
ElseStrAge = "Entered Date Of Birth is less then Current Date";
LblStatus.Text = "Youe Exact Age is : " + StrAge;
catch (Exception ex) {

Nilesh Jadhav


Post Comment:

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)

    Type the numbers and letters shown on the left.

    Submit Article     Return to Article Index
    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India