Click here to Skip to main content
12,633,990 members (24,412 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C#
I want to write a code to calculate a person's age by taking birthdate from the user. Using datetime function.
Posted 6-Apr-11 20:56pm
rinks25317
Comments
Abhinav S 7-Apr-11 13:32pm
   
What have you tried so far?
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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

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
  Permalink  
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
SAKryukov 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.
SAKryukov 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.
SAKryukov 7-Apr-11 4:56am
   
Right. And also people do not realize that date and time are not different things.
Calling a type DateTime is almost the same as to call it YearTime or MonthTime. There is just Time and Duration; the difference is essential.
--SA
Nuri Ismail 7-Apr-11 4:37am
   
Good answer. My 5. :)
SAKryukov 7-Apr-11 4:53am
   
Wow, and you want to join the club, too! :-)
You're very welcome. And thanks.
--SA
Menon Santosh 8-Sep-12 4:54am
   
nice Solution +5
   
Thank you, Menon.
--SA
Andreas Gieriet 20-Feb-13 12:06pm
   
My 5!
Andi
Sergey Alexandrovich Kryukov 20-Feb-13 14:10pm
   
Thank you, Andi.
—SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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());
  Permalink  
v2
Comments
SAKryukov 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. :)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

Using the Time Period Library[^]:
// ----------------------------------------------------------------------
public void AgeSample( DateTime birthDay )
{
  // output: x Year x Months
  Console.WriteLine( new DateDiff( birthDay ).GetDescription( 2 ) );
} // AgeSample
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Console.WriteLine("How old are you? (Please speak clearly into your monitor)");
  Permalink  
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Now that I've given everyone a chance to stop laughing...

int age = DateTime.Now.Year - birthday.Year;
  Permalink  
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
   
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 ;)
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

DateTime dob = DateTime.Parse("11 Dec 1992");
DateTime curYear = DateTime.Now.Year;
TimeSpan ts = CurYear - dob;
Sting age = ts.ToString() + "Years" ;
  Permalink  
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
Member 11411308 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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.161208.2 | Last Updated 20 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100