hi
i am new to c# . i need to get age from (current date -date of birth) in days ,months,years.
thanks in advance
3 replies to this topic
#1
Posted 18 May 2011 - 08:47 AM
|
|
|
#2
Posted 18 May 2011 - 10:09 AM
What do you have so far?
#3
Posted 18 May 2011 - 11:35 AM
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--;
}
}
}
}
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
Posted 19 May 2011 - 11:01 AM
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


Sign In
Create Account

Back to top









