Anas TINA
asked on
Display birth date as age
I need to display birthdate value as age.
Examples:
The client asks for rounding the age value but she does not give me the rules.
Is there any ready component to use instead I reinvent the wheel?
Thanks
Examples:
DateTime bd1 = new DateTime(1, 5, 1);
string age1 = bd1.AsAge();
//output:
// 1 Year, 5 Monthes, and 1 Day.
DateTime bd2 = new DateTime(50, 1, 30);
string age2 = bd2.AsAge();
//output:
// 50 Years, 1 Month, and 30 Day.
The client asks for rounding the age value but she does not give me the rules.
Is there any ready component to use instead I reinvent the wheel?
Thanks
You can start by calculating the age in full years using an extension method:
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace DateTimeExtension
{
/// <summary>
/// Extended methods for System.DateTime.
/// </summary>
public static class DateTimeExtensionMethods
{
/// <summary>
/// Count of days per week.
/// </summary>
private static int _daysInWeek = 7;
/// <summary>
/// Count of months in a quarter.
/// </summary>
private static int _monthsInQuarter = 3;
/// <summary>
/// Minimum and maximum week number according to ISO 8601.
/// </summary>
private static int _minIso8601WeekNumber = 1;
private static int _maxIso8601WeekNumber = 53;
#region Age
/// <summary>
/// Calculates the age in years of the current System.DateTime object today.
/// </summary>
/// <param name="birthDate">The date of birth.</param>
/// <returns>Age in years today. 0 is returned for a future date of birth.</returns>
public static int Age(this DateTime birthDate)
{
return Age(birthDate, DateTime.Today);
}
/// <summary>
/// Calculates the age in years of the current System.DateTime object on a later date.
/// </summary>
/// <param name="birthDate">The date of birth.</param>
/// <param name="laterDate">The date on which to calculate the age.</param>
/// <returns>Age in years on a later day. 0 is returned as minimum.</returns>
public static int Age(this DateTime birthDate, DateTime laterDate)
{
int age;
age = laterDate.Year - birthDate.Year;
if (age > 0)
{
if (laterDate.Date < birthDate.Date.AddYears(age))
{
// The last year is not a full year.
// Reduce the year count by one.
age--;
}
}
else
{
age = 0;
}
return age;
}
#endregion
// <snip>
You can easily extend this to have a method for months, and the remaining days will be the day count.
Gustav beat me to it - an extension method for DateTime is the cleanest way to do it.
ASKER
/// <summary>
/// Count of days per week.
/// </summary>
private static int _daysInWeek = 7;
/// <summary>
/// Count of months in a quarter.
/// </summary>
private static int _monthsInQuarter = 3;
/// <summary>
/// Minimum and maximum week number according to ISO 8601.
/// </summary>
private static int _minIso8601WeekNumber = 1;
private static int _maxIso8601WeekNumber = 53;
Could you please explain the above?
Sorry, I should have excluded those. You can just ignore them.
The Age extension method is just one of the class DateTimeExtensionMethods that holds several other methods where those constants are used.
The Age extension method is just one of the class DateTimeExtensionMethods that holds several other methods where those constants are used.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How do you plan to store an age of exactly 5 years, or 5 years and 1 day?
A TimeSpan isn't useful either because it stores a duration independent of a start date, so it cannot give a result in years and months.
I suggest you create your own Age structure. It's not a lot of work, and it will do exactly what you want it to do.