public static int CalculateAge(DateTime birthdate)
{
int years = DateTime.Now.Year - birthdate.Year;
if (DateTime.Now.Month < birthdate.Month
|| (DateTime.Now.Month == birthdate.Month
&& DateTime.Now.Day < birthdate.Day))
years--;
return years;
}
Asked
Active
Viewed 51 times
1

Soner Gönül
- 97,193
- 102
- 206
- 364

user3239745
- 63
- 3
-
possible duplicate of [How do I calculate someone's age in C#?](http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c) – Soner Gönül Jan 27 '14 at 08:57
-
1Not a duplicate. This question is about putting the age value in a textbox, not how to calculate it in the first place. – Deestan Jan 27 '14 at 09:00
-
1Why down-voted? it's a legitimate question! – Adam Bilinski Jan 27 '14 at 09:01
-
I am unsure how the title of the question relates to the posted code and what the OP actually wants to know. – Adam Straughan Jan 27 '14 at 09:05
-
@user3239745 did any of the provided answers help you? If yes please mark it as accepted answer (click on the tick at the up-vote and down-vote buttons) – RononDex Jan 27 '14 at 09:53
2 Answers
3
Simply do this:
textBox1.Text = Convert.ToString(CalculateAge(someDate));
You have to assign the calculated value to the Text
property of your textbox. But as the return type of your function is int
you first have to Convert it to a string
, as a TextBox can only display strings.

RononDex
- 4,143
- 22
- 39