Pages

Monday 5 December 2011

Calculate age depending on dateofbirth using c#.net

Here I will explain Calculating dateofbirth in c#.net

//Date of Birth

DateTime dateOfBirth = new DateTime(1992, 06, 10);
//Current date
DateTime currentDate = DateTime.Now;
int ageInYears = 0;
int ageInMonths = 0;
int ageInDays = 0;
//Calculate days
ageInDays = currentDate.Day - dateOfBirth.Day;
//Calculate Months
ageInMonths = currentDate.Month - dateOfBirth.Month;
//Calculate Years
ageInYears = currentDate.Year - dateOfBirth.Year;
//leaf years
if (ageInDays < 0)
{
ageInDays +=
DateTime.DaysInMonth(currentDate.Year, currentDate.Month);
ageInMonths = ageInMonths--;
if (ageInMonths < 0)
{
ageInMonths += 12;
ageInYears--;
}
}

if (ageInMonths < 0)
{
ageInMonths += 12;
ageInYears--;

Console.WriteLine("Age in Years :{0}, Months : {1}, Days : {2}", ageInYears, ageInMonths, ageInDays);

 

No comments:

Post a Comment