hey i am not an expert but try this code maybe its too lengthy and may have lots of if-nested-if loops but i think even a beginner can understand this code quickly
first i am doing this code to accept a text of date of birth in one textbox and display age on another textbox as soon as the date of birth is enetered
lets take the textbox which date of birth has to be entered as txtDateofBirth and that to display age as txtAge
so we have to display age when text is changed in txtDateofBirth
protected void txtDateofBirth_TextChanged1(object sender, EventArgs e)
{
DateTime dob = Convert.ToDateTime(txtDateofBirth.Text);
int myYear = dob.Year;
int myMonth = dob.Month;
int myDay = dob.Day;
int currentYear = DateTime.Now.Year;
int currentMonth = DateTime.Now.Month;
int currentDay = DateTime.Now.Day;
int Age = currentYear - myYear;
if (currentMonth >= myMonth)
{
if (currentMonth == myMonth)
{
if (currentDay > myDay)
{
txtAge.Text = Convert.ToString(Age);
}
else
{
txtAge.Text = Convert.ToString(Age - 1);
}
}
else
{
txtAge.Text=Convert.ToString(Age);
}
}
else
{
txtAge.Text=Convert.ToString(Age-1);
}
}
}
so you must enable autopostback of txtDateofBirth To get the age quickly
this is my first solution so sorry if it is too lengthy