15,884,750 members
See more:
hi,

how to calculate current age (Day, Month, Year) from date of birth and date of birth select from combo box?
i have three combo box and one text box first for select date second for select month and last for select year. find the current age with (Day, Month, Year) in text box.
Posted

## Solution 1

Hi!!! you do like that!!!

C#
```protected void btn_Click(object sender, EventArgs e)
{
// here you use your ddl,combobox or etc where age date is inserted
DateTime dtStart = DateTime.Parse(txtDoB.Text);
TimeSpan sp = DateTime.Now - dtStart;
int year = "18";
int days = 365;
int total = year * days;

if (sp.Days < total)
{

txtDoB.Text = "Not Valid";
lblMess.Text = "You can not be under 18 years";
lblMess.CssClass = "Errr";
return;
}
}```

its only basic idea of how to do !!!! have code in c# you do in VB.net i think it should help you!!!!!!

v3
[no name] 12-Mar-13 3:16am
Nice Try.

## Solution 3

VB
```Protected Sub btn_Click(sender As Object, e As EventArgs)
' here you use your ddl,combobox or etc where age date is inserted
Dim dtStart As DateTime = DateTime.Parse(txtDoB.Text)
Dim sp As TimeSpan = DateTime.Now - dtStart
Dim year As Integer = "18"
Dim days As Integer = 365
Dim total As Integer = year * days

If sp.Days < total Then

txtDoB.Text = "Not Valid"
lblMess.Text = "You can not be under 18 years"
Return
End If
End Sub```

v2
Anurag Sinha V 12-Mar-13 3:18am
@Onurag19....May i ask why was my code converted?

-regards

## Solution 2

Try this:
VB
```Dim strDOB As String
strDOB = txtDateOfBirth.TextDim
intAge As Integer
intAge = Math.Floor(DateDiff(DateInterval.Month, DateValue(strDOB), Now()) / 12)
lblAge.Text = intAge```

## Solution 4

C#
```protected void btn_Click(object sender, EventArgs e)
{
string dt=combobox1.selectedvalue+"/"+combobox2.selectedvalue+"/"+
combobox3.selectedvalue;
System.DateTime dateOfBirth=Convert.ToDateTime(dt);
TimeSpan ts=new TimeSpan();
System.DateTime currentDate=DateTime.Now();
ts=currentDate.Subtract(dateOfBirth);
int years=0;
int months=0;
int days=0;
years=(ts.Days/366);

DateTime workingDate = dateOfBirth.AddYears(years);

//calculation of months
ts= currentDate - workingDate;
months = timeSpan.Days / 31;

//calculation of days

TextBox1.Text=concatenate the values of years,months and days which you get from above.

}```

Also, you need to take into considerations some special conditions such as the date of birth is 31/12/2012 and the currentdate is 01/01/2013...some conditions need to be checked...have a look at that...I dont remember those now.

The above code will accept values from all the 3 combo boxes in a string variable and then a TimeSpan object will calculate the diff between current date and the date of birth.
Also the highest entity which TimeSpan() shows is the number of days..Hence we need to multiply the TimeSpan diff with enitity "Days" and divide it by 366 to get the number of years..

Hope the above helps...

Converted In VB
VB
```Protected Sub btn_Click(sender As Object, e As EventArgs)
Dim dt As String = (combobox1.selectedvalue + combobox2.selectedvalue &amp; &quot;/&quot;) + combobox3.selectedvalue
Dim dateOfBirth As System.DateTime = Convert.ToDateTime(dt)
Dim ts As New TimeSpan()
Dim currentDate As System.DateTime = DateTime.Now()
ts = currentDate.Subtract(dateOfBirth)
Dim years As Integer = 0
Dim months As Integer = 0
Dim days As Integer = 0
years = (ts.Days / 366)
Dim workingDate As DateTime = dateOfBirth.AddYears(years)
ts = currentDate - workingDate
months = timeSpan.Days / 31
End Sub```

regards
-anurag

v4
[no name] 12-Mar-13 3:21am
Because the answer wanted has to be in VB.
Thanks
Anurag Sinha V 12-Mar-13 3:26am
Ohh..I saw it just now..Thanks for noticing that...:)

-Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 CPallini 155 k5054 105 M-Badger 80 OriginalGriff 70 Richard MacCutchan 30
 Pete O'Hanlon 2,065 OriginalGriff 1,565 Graeme_Grant 1,030 Richard Deeming 893 Dave Kreskowiak 744

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900