Calculate a person's age
- Does anyone have an elegant, accurate way of calculating a person's age given today's date and time and the person's date of birth date?
Answers
TimeSpan won't do it. There's no property for Years.
Here's a couple of ways:
1. Use a while loop
int age = 0;
while (DateTime.Today.AddYears(-(i + 1)) >= birthDay)
age++;
2. Use another more detailed method:
//// get the raw years
DateTime birthDay = new DateTime(1979, 11, 4);
var age = DateTime.Today.Year - birthDay.Year;// adjust for whether the birthday is past or not.
DateTime thisYearsBirthDay = new DateTime(DateTime.Today.Year, birthDay.Month, birthDay.Day);
age -= DateTime.Today >= thisYearsBirthDay ? 0 : 1;
Console.WriteLine(age);
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byRoahn LuoMSFT, ModeratorFriday, November 06, 2009 9:40 AM
- Hi,
You can use the following class also.
class Age { public Age(DateTime dateOfBirth) { CalculateAge(dateOfBirth,DateTime.Now); } public Age(DateTime dateOfBirth, DateTime dateToSubstractFrom) { CalculateAge(dateOfBirth, dateToSubstractFrom); } public int Year { get; set; } public int Month { get; set; } public int Day { get; set; } public int Hour { get; set; } public int Minute { get; set; } public int Second { get; set; } private void ClearValue() { Year = 0; Month = 0; Day = 0; Hour = 0; Minute = 0; Second = 0; } private void CalculateAge(DateTime dateOfBirth,DateTime dateToSubstractFrom) { ClearValue(); DateTime internalDateOfBirth = dateOfBirth; while (internalDateOfBirth.AddYears(1) <= dateToSubstractFrom) { this.Year++; internalDateOfBirth = internalDateOfBirth.AddYears(1); } while (internalDateOfBirth.AddMonths(1) <= dateToSubstractFrom) { this.Month++; internalDateOfBirth = internalDateOfBirth.AddMonths(1); } while (internalDateOfBirth.AddDays(1) <= dateToSubstractFrom) { this.Day++; internalDateOfBirth = internalDateOfBirth.AddDays(1); } while (internalDateOfBirth.AddHours(1) <= dateToSubstractFrom) { this.Hour++; internalDateOfBirth = internalDateOfBirth.AddHours(1); } while (internalDateOfBirth.AddMinutes(1) <= dateToSubstractFrom) { this.Minute++; internalDateOfBirth = internalDateOfBirth.AddMinutes(1); } while (internalDateOfBirth.AddSeconds(1) <= dateToSubstractFrom) { this.Second++; internalDateOfBirth = internalDateOfBirth.AddSeconds(1); } } }- Marked As Answer byRoahn LuoMSFT, ModeratorFriday, November 06, 2009 9:40 AM
- Edited byTamer OzMVPThursday, November 05, 2009 5:01 PMCode fix
All Replies
- Use the TimeSpan Structure to achieve what you want. You can extract days, minutes or even years. Check out the example in MSDN. GL HTH
William Wegerson (www.OmegaCoder.Com) TimeSpan won't do it. There's no property for Years.
Here's a couple of ways:
1. Use a while loop
int age = 0;
while (DateTime.Today.AddYears(-(i + 1)) >= birthDay)
age++;
2. Use another more detailed method:
//// get the raw years
DateTime birthDay = new DateTime(1979, 11, 4);
var age = DateTime.Today.Year - birthDay.Year;// adjust for whether the birthday is past or not.
DateTime thisYearsBirthDay = new DateTime(DateTime.Today.Year, birthDay.Month, birthDay.Day);
age -= DateTime.Today >= thisYearsBirthDay ? 0 : 1;
Console.WriteLine(age);
Coding Light - Illuminated Ideas and Algorithms in Software
Coding Light Wiki • LinkedIn • ForumsBrowser- Marked As Answer byRoahn LuoMSFT, ModeratorFriday, November 06, 2009 9:40 AM
- David is right that timespan doesn't give years...but that is a simple calculation Days/365. :-)
William Wegerson (www.OmegaCoder.Com) - Don't forget leap years Omega. The DateTime method is a lot easier if you want to be accurate.
- Hi,
You can use the following class also.
class Age { public Age(DateTime dateOfBirth) { CalculateAge(dateOfBirth,DateTime.Now); } public Age(DateTime dateOfBirth, DateTime dateToSubstractFrom) { CalculateAge(dateOfBirth, dateToSubstractFrom); } public int Year { get; set; } public int Month { get; set; } public int Day { get; set; } public int Hour { get; set; } public int Minute { get; set; } public int Second { get; set; } private void ClearValue() { Year = 0; Month = 0; Day = 0; Hour = 0; Minute = 0; Second = 0; } private void CalculateAge(DateTime dateOfBirth,DateTime dateToSubstractFrom) { ClearValue(); DateTime internalDateOfBirth = dateOfBirth; while (internalDateOfBirth.AddYears(1) <= dateToSubstractFrom) { this.Year++; internalDateOfBirth = internalDateOfBirth.AddYears(1); } while (internalDateOfBirth.AddMonths(1) <= dateToSubstractFrom) { this.Month++; internalDateOfBirth = internalDateOfBirth.AddMonths(1); } while (internalDateOfBirth.AddDays(1) <= dateToSubstractFrom) { this.Day++; internalDateOfBirth = internalDateOfBirth.AddDays(1); } while (internalDateOfBirth.AddHours(1) <= dateToSubstractFrom) { this.Hour++; internalDateOfBirth = internalDateOfBirth.AddHours(1); } while (internalDateOfBirth.AddMinutes(1) <= dateToSubstractFrom) { this.Minute++; internalDateOfBirth = internalDateOfBirth.AddMinutes(1); } while (internalDateOfBirth.AddSeconds(1) <= dateToSubstractFrom) { this.Second++; internalDateOfBirth = internalDateOfBirth.AddSeconds(1); } } }- Marked As Answer byRoahn LuoMSFT, ModeratorFriday, November 06, 2009 9:40 AM
- Edited byTamer OzMVPThursday, November 05, 2009 5:01 PMCode fix


