Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

How to calculate age based on "date time picker selection" in windows application with c# coding.


Thanks and Regards,
Murali.
Posted
Updated 30-Dec-12 23:59pm
v2

C#
int myAge = 
DateTime.Today.Year - dateTimePicker1.Value.Year; // CurrentYear - YourBirthDate
 
 textBox1.Text = myAge.ToString();
 
Share this answer
 
v3
Comments
Thomas Daniels 31-Dec-12 6:49am    
I'm not sure about this. If 'Today' is 30 Dec 2012, and if your birth date is 31 Dec 2011, then your output will be '1'. But that's not correct, because '1' is only correct from 31 Dec 2012 up to and including 30 Dec 2013.
[no name] 31-Dec-12 7:18am    
Thanks you. It's working fine.
Hi,

Have a look here:
Calculating Duration Between Two Dates in Years, Months and Days[^]
To get the value (DateTime) from DateTimePicker, try this:
C#
DateTime dt = dateTimePicker1.Value;
 
Share this answer
 
v2
Try this..

C#
TimeSpan ts=DateTime.Now - Convert.ToDateTime(datePicker.SelectedDate);
int age = Convert.ToInt32(ts.Days) / 365;


hope this will heip you.
 
Share this answer
 

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