anyone know how to get the age based on a date(birthdate)
im thinking of something like this
string age = DateTime.Now.GetAccurateAge();
public static class DateTimeExtensions
{
public static string ToAgeString(this DateTime dob)
{
DateTime today = DateTime.Today;
int months = today.Month - dob.Month;
int years = today.Year - dob.Year;
if (today.Day < dob.Day)
{
months--;
}
if (months < 0)
{
years--;
months += 12;
}
int days = (today - dob.AddMonths((years * 12) + months)).Days;
return string.Format("{0} year{1}, {2} month{3} and {4} day{5}",
years, (years == 1) ? "" : "s",
months, (months == 1) ? "" : "s",
days, (days == 1) ? "" : "s");
}
}
Not certain that it would always be correct (haven't thought about if there's some cases with leap years etc that might make it fail...), but this is an easy way to get out the year and month:
DateTime bd = DateTime.Parse("2009-06-17");
TimeSpan ts = DateTime.Now.Subtract(bd);
DateTime age = DateTime.MinValue + ts;
string s = string.Format("{0} Years {1} months {2} days", age.Year -1 , age.Month - 1, age.Day - 1);
To get the age I would use the datetime of the birthdate and find the difference between that and the current system time. This link shows how to find the difference between two datetimes. Just make the starttime be the user's birthdate and endtime be now (DateTime.Now;)
I find this the most accurate.
private int GetAge(DateTime birthDate)
{
TimeSpan ageTimeSpan = DateTime.UtcNow.Subtract(birthDate);
int age = new DateTime(ageTimeSpan.Ticks).Year;
return age;
}
I was noticing peculiarities with LukeH's answer and leap years. The simplest example is probably a dob = 2/29/2016
as of 2/28/2017
and 3/1/2017
. The way I look at it, there are no days left in Feb 2016, there are 11 complete months (Mar-Jan) in between, and there have been 28 days in Feb 2017 so far, so as of 2/28/2017
, I would call this person 11 months 28 days old. And 1 year 1 day old as of 3/1/2017
. (Although some leap day babies do celebrate and on the 28th of common years ... I'm curious what the legal consideration is.)
Because LukeH's method makes uses of DateTime.AddMonths
, it calculates 11 months 30 days, because it finds the difference in days between 2/28/2017
and 1/29/2017
. So it calculates that 2/28/2017
is 11 mo 30 days after 2/29/2016
, and 11 mo 27 days after 3/1/2016
. A 3 day age difference for birthdays only 1 day apart. If it came up with 28 days as I did "by hand" they would have a one day age difference.
I'm not exactly sure how to describe the fundamental difference in approaches, but here is my stab at it. It seems there is always a gotcha with dates so please scrutinize.
internal static void CalculateAge(DateTime dateOfBirth, DateTime asOfDate, out int years, out int months, out int days)
{
Console.Write("As of " + asOfDate.ToShortDateString() + ": ");
//Get the relative difference between each date part
days = asOfDate.Day - dateOfBirth.Day;
months = asOfDate.Month - dateOfBirth.Month;
years = asOfDate.Year - dateOfBirth.Year;
if (days < 0)
{
days = DateTime.DaysInMonth(dateOfBirth.Year, dateOfBirth.Month) - dateOfBirth.Day + //Days left in month of birthday +
asOfDate.Day; //Days passed in asOfDate's month
months--; //Subtract incomplete month that was already counted
}
if (months < 0)
{
months += 12; //Subtract months from 12 to convert relative difference to # of months
years--; //Subtract incomplete year that was already counted
}
Console.WriteLine(string.Format("{0} year{1}, {2} month{3} and {4} day{5}",
years, (years == 1) ? "" : "s",
months, (months == 1) ? "" : "s",
days, (days == 1) ? "" : "s"));
}
This is what I use. It's a combination of the selected answer and this answer.
For someone born on 2016-2-29, on the 2017-3-1 their age outputs:
Years: 1
Months: 1 (28 days for February)
Days: 0
var today = new DateTime(2020,11,4);
//var today = DateTime.Today;
// Calculate the age.
var years = today.Year - dateOfBirth.Year;
// Go back to the year in which the person was born in case of a leap year
if (dateOfBirth.Date > today.AddYears(-years))
{
years--;
}
var months = today.Month - dateOfBirth.Month;
// Full month hasn't completed
if (today.Day < dateOfBirth.Day)
{
months--;
}
if (months < 0)
{
months += 12;
}
Years = years;
Months = months;
Days = (today - dateOfBirth.AddMonths((years * 12) + months)).Days;