Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI I am using three drop downs to select the date the drop downs look return the values 1998 september 12

i am trying to calculate the age but get an error

C#
ddlMonth.DataSource = Enumerable.Range(1, 12).Select(a => new
               {
                   MonthName = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
                   MonthNumber = a
                   //DataTextField = DateTimeFormatInfo.CurrentInfo.GetMonthName(a),
                   //DataValueField = a
               }
               );

               //If this code is not working then try the above code in the loop.
               ddlMonth.DataTextField = "MonthName";
               ddlMonth.DataValueField = "MonthNumber";

               ddlMonth.DataBind();
               ddlYear.DataSource = Enumerable.Range(DateTime.Now.Year - 99, 100).Reverse();
               ddlYear.DataBind();
               ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedValue)));
               ddlday.DataBind();
           }

       }
       protected void ddlMonth_SelectedIndexChanged(object sender, EventArgs e)
       {
           ddlday.DataSource = Enumerable.Range(1, DateTime.DaysInMonth(DateTime.Now.Year, Convert.ToInt32(ddlMonth.SelectedIndex + 1))); ddlday.DataBind();
       }

C#
private int _GetAge()
      {
          //int Age = Convert.ToInt32((DateTime.Now.Subtract().TotalDays / 365.25).ToString("#"));

          //ddlYear.SelectedValue.ToString();
          //ddlMonth.SelectedValue.ToString();
          //ddlday.SelectedValue.ToString();

          string age = (ddlYear.SelectedItem.ToString() + ddlMonth.SelectedItem.ToString() + ddlday.SelectedItem.ToString());

          age.Substring(2, age.Length - 2);
          //age = age.PadRight(13, 0);

          IDValidator dateSelect = new IDValidator(age.ToString());

          if (!dateSelect.get_IsValidDate(true))
              throw new ArgumentException("Date not Valid");
          return dateSelect.get_Age(DateTime.Now);


      }
Posted
Comments
[no name] 30-Aug-12 9:04am    
Are we supposed to guess what "an error" is?
Legor 30-Aug-12 9:06am    
Whats the error. Improve the question please.
ridoy 30-Aug-12 10:01am    
what is your error?

1 solution

C#
DateTime today = DateTime.Today;  //  08/30/2012
DateTime birthday = DateTime.MinValue;
DateTime.TryParse(age, out birthday);//  08/30/2000
string actual_age = string.Empty;
if (birthday.DayOfYear <= today .DayOfYear)
   {
       actual_age  = (today .Year-birthday.Year).ToString();
   }
   else
   {
       actual_age  = (today .Year-birthday.Year- 1).ToString();
   }


Hope it helps.
 
Share this answer
 
v5
Comments
Christian Amado 30-Aug-12 9:11am    
@MarkNinja This is the simple way to get the age. The age variable you declared above as string =)
Christian Amado 30-Aug-12 12:00pm    
Sorry the version1 had an error. TotalYears doesn't exists.

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