Click here to Skip to main content
15,884,750 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please help me

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.

Hi!!! you do like that!!!

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";

its only basic idea of how to do !!!! have code in c# you do in i think it should help you!!!!!!
Share this answer
[no name] 12-Mar-13 3:16am    
Nice Try.
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"
    End If
End Sub

Hope I help you.
Share this answer
Anurag Sinha V 12-Mar-13 3:18am    
@Onurag19....May i ask why was my code converted?

Try this:
Dim strDOB As String
strDOB = txtDateOfBirth.TextDim 
intAge As Integer
intAge = Math.Floor(DateDiff(DateInterval.Month, DateValue(strDOB), Now()) / 12)
lblAge.Text = intAge
Share this answer
protected void btn_Click(object sender, EventArgs e)
string dt=combobox1.selectedvalue+"/"+combobox2.selectedvalue+"/"+
System.DateTime dateOfBirth=Convert.ToDateTime(dt);
TimeSpan ts=new TimeSpan();
System.DateTime currentDate=DateTime.Now();
int years=0;
int months=0;
int days=0;

DateTime workingDate = dateOfBirth.AddYears(years);

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

//calculation of days
days = currentDate.Subtract(dateOfBirth.AddYears(years).AddMonths(months)).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
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
    workingDate = workingDate.AddMonths(months)
    days = currentDate.Subtract(dateOfBirth.AddYears(years).AddMonths(months)).Days
End Sub

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


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

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