Click here to Skip to main content
15,886,004 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
sir i am using three dropdownlist and and one text box in three dropdownlist i am showing date month year
i want to calculate age using all dropdownlist and display total age in the text box
Posted
Updated 12-May-16 23:37pm
v2

  1. Create a DateTime object based on the DropDownList entries, say dtALongTimeAgo
  2. Get the TimeSpan object, say tsElapsed, as the difference between DateTime.Today and dtALongTimeAgo
  3. Fell free to use the tsElapsed properties in order to retrieve all the info you need.
 
Share this answer
 
try this, customize it based on your need.

ASP.NET
Day:<asp:DropDownList runat="server" ID="ddlD">
      </asp:DropDownList>
      Month:<asp:DropDownList runat="server" ID="ddlM">
      </asp:DropDownList>
      Year:<asp:DropDownList runat="server" ID="ddlY">
      </asp:DropDownList>


      <asp:Label runat="server" ID="lblAge"></asp:Label>
      <asp:Button ID="btnCalculate" runat="server" Text="Calculate" OnClick="btnCalculate_Click" />



CS

C#
protected void Page_Load(object sender, EventArgs e)
        {
             

            if (!IsPostBack)
            { 
                for (int i = 1; i <= 31; i++)
                    ddlD.Items.Add(i.ToString());

                for (int i = 1; i <= 12; i++)
                    ddlM.Items.Add(i.ToString());

                for (int i = 1; i <= 75; i++)
                    ddlY.Items.Add((DateTime.Now.Year - i).ToString());
                 
            }
        }
       

        protected void btnCalculate_Click(object sender, EventArgs e)
        {
            int day,month,year ;
            int.TryParse(ddlD.Text, out day);
            int.TryParse(ddlM.Text, out month);
            int.TryParse(ddlY.Text, out year);

            DateTime target = new DateTime(year, month, day);
            DateTime now = DateTime.Now;
            Age age = new Age(target, now);
            lblAge.Text = string.Format(" {0} years, {1} months, and {2} days", age.Years, age.Months, age.Days);
            
        }


used this[^]code for calculating the age, just add it in your code
 
Share this answer
 
Comments
Member 12513529 13-May-16 5:54am    
Age age = new Age(target, now);
error comming in this line
Karthik_Mahalingam 13-May-16 5:59am    
you have to create a class and copy paste the code from
this link[^]
Karthik_Mahalingam 13-May-16 5:59am    
public class Age
{
public int Years;
public int Months;
public int Days;

public Age(DateTime Bday)
{
this.Count(Bday);
}

public Age(DateTime Bday, DateTime Cday)
{
this.Count(Bday, Cday);
}

public Age Count(DateTime Bday)
{
return this.Count(Bday, DateTime.Today);
}

public Age Count(DateTime Bday, DateTime Cday)
{
if ((Cday.Year - Bday.Year) > 0 ||
(((Cday.Year - Bday.Year) == 0) && ((Bday.Month < Cday.Month) ||
((Bday.Month == Cday.Month) && (Bday.Day <= Cday.Day)))))
{
int DaysInBdayMonth = DateTime.DaysInMonth(Bday.Year, Bday.Month);
int DaysRemain = Cday.Day + (DaysInBdayMonth - Bday.Day);

if (Cday.Month > Bday.Month)
{
this.Years = Cday.Year - Bday.Year;
this.Months = Cday.Month - (Bday.Month + 1) + Math.Abs(DaysRemain / DaysInBdayMonth);
this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
}
else if (Cday.Month == Bday.Month)
{
if (Cday.Day >= Bday.Day)
{
this.Years = Cday.Year - Bday.Year;
this.Months = 0;
this.Days = Cday.Day - Bday.Day;
}
else
{
this.Years = (Cday.Year - 1) - Bday.Year;
this.Months = 11;
this.Days = DateTime.DaysInMonth(Bday.Year, Bday.Month) - (Bday.Day - Cday.Day);
}
}
else
{
this.Years = (Cday.Year - 1) - Bday.Year;
this.Months = Cday.Month + (11 - Bday.Month) + Math.Abs(DaysRemain / DaysInBdayMonth);
this.Days = (DaysRemain % DaysInBdayMonth + DaysInBdayMonth) % DaysInBdayMonth;
}
}
else
{
throw new ArgumentException("Birthday date must be earlier than current date");
}
return this;
}
}

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