Jump to content

NEED Help In Fetching Date Of Birth

- - - - -

  • Please log in to reply
3 replies to this topic

#1
rms

rms

    Newbie

  • Members
  • Pip
  • 2 posts
hi
i am new to c# . i need to get age from (current date -date of birth) in days ,months,years.

thanks in advance

#2
PGP_Protector

PGP_Protector

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 253 posts
What do you have so far?

#3
rms

rms

    Newbie

  • Members
  • Pip
  • 2 posts
hi

this is wt i hve done if i give directly the dob it is coming.but i want it as user enterable.
class Program
{
static void Main(string[] args)
{

DateTime dateOfBirth =Console.ReadKey() ;
Console.WriteLine("Enter The DOB:{0}", dateOfBirth);
//DateTime dateOfBirth = dateOfBirth.ToString();
//= new DateTime(1988, 3, 2);
int ageInYears = 0;
int ageInMonths = 0;
int ageInDays = 0;
CalculateAge(dateOfBirth,out ageInYears, out ageInMonths, out ageInDays);
Console.WriteLine("{0}, {1}, {2}", ageInYears, ageInMonths, ageInDays);
}

static void CalculateAge(DateTime DateOfBirth, out int NoOfYears, out int NoOfMonths, out int NoOfDays)
{
DateTime CurrentDate = DateTime.Now;
NoOfDays = CurrentDate.Day - DateOfBirth.Day;
NoOfMonths = CurrentDate.Month - DateOfBirth.Month;
NoOfYears = CurrentDate.Year - DateOfBirth.Year;
if (NoOfDays==0)
{
NoOfDays += DateTime.DaysInMonth(DateOfBirth.Year,DateOfBirth.Month);
NoOfMonths--;
}


if (NoOfMonths==0)
{
NoOfMonths += 12;
NoOfYears--;
}


}
}
}

#4
fayyazlodhi

fayyazlodhi

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 403 posts
You could use the following


// Get difference timespan

TimeSpan elapsed = currentDate.Subtract(dateOfBirth );


// Obtain the number of days since birth and similarly other fields

double daysAgo = elapsed.TotalDays;






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users