Click here to Skip to main content
15,884,047 members
Please Sign up or sign in to vote.
2.00/5 (4 votes)
See more:
I want to write a code to calculate a person's age by taking birthdate from the user. Using datetime function.
Posted
Comments
Abhinav S 7-Apr-11 13:32pm    
What have you tried so far?

Hm. How come the MSDN help did not tell you that? It looks so obvious:

C#
System.DateTime birthTime = AskTheUser(myUser); // :-)
System.DateTime now = System.DateTime.Now;
System.TimeSpan age = now - birthTime; //as simple as that
double ageInDays = age.TotalDays; // will you convert to whatever you want yourself?


—SA
 
Share this answer
 
v2
Comments
HaBiX 7-Apr-11 3:45am    
3.70 / 5, 6 votes Vote

lot of users dont see the benefit of timespan -_-"
5 from me
Sergey Alexandrovich Kryukov 7-Apr-11 4:18am    
It's great I found one who does. :-) Thank you very much.
--SA
CPallini 7-Apr-11 4:21am    
My 5.
Sergey Alexandrovich Kryukov 7-Apr-11 4:23am    
Decided to join the club? :-) (I refer to the comments above.)
You're very welcome. And thank you.
--SA
CPallini 7-Apr-11 4:29am    
Well, people don't realize time and time-interval are different types.
DateTime birthdate = new DateTime(1981, 1, 7);
DateTime now = DateTime.Now;
int years = now.Year - birthdate.Year;
if (now.Month < birthdate.Month || (now.Month == birthdate.Month && now.Day < birthdate.Day))
  years--;
MessageBox.Show(years.ToString());
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 7-Apr-11 2:04am    
Well, do you think integer number of years is required? :-) Good. My 5.
I answered, too.
--SA
Nuri Ismail 7-Apr-11 4:41am    
Personally I'd use TimeSpan (already suggested by SA)
but your answer is also good. My 5. :)
Using the Time Period Library[^]:
// ----------------------------------------------------------------------
public void AgeSample( DateTime birthDay )
{
  // output: x Year x Months
  Console.WriteLine( new DateDiff( birthDay ).GetDescription( 2 ) );
} // AgeSample
 
Share this answer
 
C#
Console.WriteLine("How old are you? (Please speak clearly into your monitor)");
 
Share this answer
 
v2
Comments
Andreas Gieriet 20-Feb-13 12:09pm    
My 5!
Does not directly help, but I like it :-)
An alternative is: "Please enter your age" to get the age. ;-)
Seriously, sometimes, one forgets considering the obviously simplest solution!
Cheers
Andi
Now that I've given everyone a chance to stop laughing...

C#
int age = DateTime.Now.Year - birthday.Year;
 
Share this answer
 
Comments
Michael Bookatz 7-Apr-11 18:49pm    
Hi John,

Not sure this will work as what happens if it's Jan and the birthday is in Dec? You will make him 11 months older then he is.

Regards
#realJSOP 8-Apr-11 8:58am    
You have my permission to make any adjustments that you see fit, including doing the timespan calculation and get the days and divide by 365.
Jani Giannoudis 8-Apr-11 12:02pm    
see solution 4 ;)
C#
DateTime dob = DateTime.Parse("11 Dec 1992");
DateTime curYear = DateTime.Now.Year;
TimeSpan ts = CurYear - dob;
Sting age = ts.ToString() + "Years" ;
 
Share this answer
 
Comments
SoMad 20-Feb-13 12:45pm    
I think you should run a calculation on the age of this question. I don't think it was necessary to resurrect it after all this time.

Soren Madsen
Shrikesh_kale 7-Mar-15 0:45am    
any one help me for age calculate As year , month, day
Surendra0x2 11-Mar-15 8:15am    
https://raasukutty.wordpress.com/2009/06/18/c-calculate-age-in-years-month-and-days/
https://gist.github.com/faisalman/1724253

refer these links...

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