anyone know how to get the age based on a date(birthdate)
im thinking of something like this
string age = DateTime.Now.GetAccurateAge();
This sounds like a fine exercise to get to know the TimeSpan class better.
Since i can't post code to a comment, here it is the code based on @LukeH answer, with the bug fixed
public static int GetAge( DateTime dob, DateTime today, out int days, out int months ) {
DateTime dt = today;
if( dt.Day < dob.Day ) {
dt = dt.AddMonths( -1 );
}
months = dt.Month - dob.Month;
if( months < 0 ) {
dt = dt.AddYears( -1 );
months += 12;
}
int years = dt.Year - dob.Year;
var offs = dob.AddMonths( years * 12 + months );
days = (int)( ( today.Ticks - offs.Ticks ) / TimeSpan.TicksPerDay );
return years;
}