15,920,896 members
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?

## Solution 2

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.

v2

## Solution 4

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;
}```

## Solution 5

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;

v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Top Experts
Last 24hrsThis month
 OriginalGriff 70 k5054 65 Jo_vb.net 60 Pete O'Hanlon 50 GamersLube LLC. 20
 Pete O'Hanlon 960 OriginalGriff 861 Dave Kreskowiak 475 Richard MacCutchan 320 Richard Deeming 280

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