Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In my project, I want to calculate the age of students by their date of birth.The age should calculate automatically when they select the year in the dropdown list control and it should be displayed in a textbox near that.How to do this?
Posted
Comments
Raul Iloc 31-Jan-15 7:36am    
Did you check my solution?

Simplest solution to the problem:
First set the dropdownlist to autopostback and add eventhandler for OnSelectedIndexChanged
In your eventhandler, get the SelectedValue of the dropdownlist and convert it to int(year)
Then do subtract the value from DateTime.UtcNow, and subtract the result.Years to the txtBox.Text property.
 
Share this answer
 
 
Share this answer
 
v2
You should use a code similar with the next C# code:
C#
int GetAge(DateTime birthDate)
{
  DateTime temp = DateTime.Now.AddTicks(-1 * birthDate.Ticks);
  return temp.Year;
}
 
Share this answer
 
calculate using TimeSpan like....

DateTime dob = .....
DateTime Today = DateTime.Now;
TimeSpan ts = Today - dob;
DateTime Age = DateTime.MinValue + ts;

subtraction due to min value

int Years = Age.Year - 1;
int Months = Age.Month - 1;
int Days = Age.Day - 1;
 
Share this answer
 
v2

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