Calculate age as years, month & days

In following Code Segment will calculate age and returns Age as YEARS, MONTH, and DAYS. Input to this function is Date of Birth.



public bool GetAge(DateTime DOB, out int Years, out int Month, out int Days)
{
if (DOB > DateTime.Now)
{
TimeSpan dateDiff = DateTime.Now - DOB;
DateTime age = new DateTime(dateDiff.Ticks);

Years = age.Year - 1;
Month = age.Month - 1;
Days = age.Day - 1;
return true;
}
else
{
return false;
}

}


Comments

Author: Phagu Mahato05 Feb 2014 Member Level: Gold   Points : 10

You can also try below code snippet for Calculate Age


using System;
public class Age
{
public int Years;
public int Months;
public int Days;
public Age(DateTime BirthDay)
{
this.Count(BirthDay);
}
public Age(DateTime BirthDay, DateTime CurrentDay)
{
this.Count(BirthDay, CurrentDay);
}
public Age Count(DateTime BirthDay)
{
return this.Count(BirthDay, DateTime.Today);
}
public Age Count(DateTime BirthDay, DateTime CurrentDay)
{
if ((CurrentDay.Year - BirthDay.Year) > 0 ||
(((CurrentDay.Year - BirthDay.Year) == 0) && ((BirthDay.Month < CurrentDay.Month) ||
((BirthDay.Month == CurrentDay.Month) && (BirthDay.Day <= CurrentDay.Day)))))
{
int DaysInBirthDayMonth = DateTime.DaysInMonth(BirthDay.Year, BirthDay.Month);
int DaysRemain = CurrentDay.Day + (DaysInBirthDayMonth - BirthDay.Day);
if (CurrentDay.Month > BirthDay.Month)
{
this.Years = CurrentDay.Year - BirthDay.Year;
this.Months = CurrentDay.Month - (BirthDay.Month + 1) + Math.Abs(DaysRemain / DaysInBirthDayMonth);
this.Days = (DaysRemain % DaysInBirthDayMonth + DaysInBirthDayMonth) % DaysInBirthDayMonth;
}
else if (CurrentDay.Month == BirthDay.Month)
{
if (CurrentDay.Day >= BirthDay.Day)
{
this.Years = CurrentDay.Year - BirthDay.Year;
this.Months = 0;
this.Days = CurrentDay.Day - BirthDay.Day;
}
else
{
this.Years = (CurrentDay.Year - 1) - BirthDay.Year;
this.Months = 11;
this.Days = DateTime.DaysInMonth(BirthDay.Year, BirthDay.Month) - (BirthDay.Day - CurrentDay.Day);
}
}
else
{
this.Years = (CurrentDay.Year - 1) - BirthDay.Year;
this.Months = CurrentDay.Month + (11 - BirthDay.Month) + Math.Abs(DaysRemain / DaysInBirthDayMonth);
this.Days = (DaysRemain % DaysInBirthDayMonth + DaysInBirthDayMonth) % DaysInBirthDayMonth;
}
}
else
{
throw new ArgumentException("Birthday date must be earlier than current date");
}
return this;
}
}
Or another code as below

DateTime currentDate = DateTime.Now;

int Days = currentDate.Day - dateOfBirth.Day;
int Months = currentDate.Month - dateOfBirth.Month;
int Years = currentDate.Year - dateOfBirth.Year;

if (Days < 0)
{
Days += DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
Months--;
}

if (Months < 0)
{
Months += 12;
Years--;
}

Console.WriteLine("Your Age: {0} years and {1} months and {2} days", Days, Months, Years);



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: