Important alert: (current site time 7/15/2013 8:29:00 AM EDT)
 

article

Age Calc

Email
Submitted on: 1/21/2004 7:24:21 AM
By: Elfar Alfreðsson 
Level: Beginner
User Rating: By 3 Users
Compatibility: C#, ASP.NET
Views: 10936
author picture
(About the author)
 
     heh.. just saw that previous postings were either not even .net or just.. uhm bigger? :p use, abuse & at will :p

 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
public static string GetAge(string strBDay)
{
// current date.
string strCDate = DateTime.Now.ToShortDateString();
// get year born & current year.
int yearborn = Convert.ToDateTime(strBDay).Year;
int yearnow = Convert.ToDateTime(strCDate).Year;
int totalyears = yearnow - yearborn;
// split bday up.
int strBDay_day = Convert.ToDateTime(strBDay).Day;
int strBDay_month = Convert.ToDateTime(strBDay).Month;
// get how many days since or till the bday this year.
TimeSpan hadbday = new TimeSpan();
string thisyearbday = Convert.ToString(strBDay_day + "." + strBDay_month + "." + yearnow);
hadbday = Convert.ToDateTime(strCDate) - Convert.ToDateTime(thisyearbday);
// get age.
int age = (totalyears - 1);
if(hadbday.TotalDays >= 0)
{
age++;
}
// return result.
return Convert.ToString(age);
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Beginner category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

6/30/2005 4:36:26 PM

How about this - it is shorter :)

public static int CurrentAge(int year, int month, int day) {
DateTime currentDate = DateTime.Today;
TimeSpan daysFromTillBDay;
int age = currentDate.Year - year - 1;

daysFromTillBDay = currentDate - new DateTime(currentDate.Year, month, day);
if (daysFromTillBDay.TotalDays >= 0) {
age++;
}

return age;
}

Only real comment is why are you doing so many conversions back and forth between strings and datetimes?
(If this comment was disrespectful, please report it.)

 
4/24/2006 1:24:40 AMtri dung

12:24
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.