Click here to Skip to main content
15,557,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
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.
Posted

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!!!!!!
 
Share this answer
 
v3
Comments
[no name] 12-Mar-13 3:16am    
Nice Try.
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


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

-regards
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
 
Share this answer
 
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;
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
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




regards
-anurag
 
Share this answer
 
v4
Comments
[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)



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