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...
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.
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.
You may link to this article from another website, but ONLY if it is not wrapped in a frame.
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);
}
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.)
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.)