Please can you help me in console application.
How to calculate age from DOB
class Personal{
public DateTime DateOfBirth { get; set; }
public string Age { get; set; }
}
Personal per=new Personal();
Console.WriteLine("Enter the Date Of Birth :",per.DateOfBirth);
per.DateOfBirth = DateTime.Parse(Console.ReadLine());
public string GetAge(DateTime DateOfBirth)
{
}
How can i write for function to calculate age from DOB and display
Console.WriteLine("The DOB is" +DateOfBirth);
Console.WriteLine("the Age is ...." +Age);
DOB is 04/02/1994
i want display age is 19 years, 6 months, and 9 days
Thanks.
Loading
Sanjeeb LenkaPosted Aug 14, 2013, 5:18 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IfExample
{
class DateTimeExtensions
{
public string ToAgeString( DateTime dob)
{
DateTime dt = DateTime.Now;
int days = dt.Day - dob.Day;
if (days < 0)
{
dt = dt.AddMonths(-1);
days += DateTime.DaysInMonth(dt.Year, dt.Month);
}
int months = dt.Month - dob.Month;
if (months < 0)
{
dt = dt.AddYears(-1);
months += 12;
}
int years = dt.Year - dob.Year;
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");
}
static void Main()
{
DateTimeExtensions obj = new DateTimeExtensions();
Console.WriteLine("Enter your date of birth in dd/MM/yyyy format to calculate age");
string strDate = DateTime.ParseExact(Console.ReadLine(), "dd/MM/yyyy", null).ToString("MM/dd/yyyy");
DateTime dob = DateTime.Parse(strDate);
string s = obj.ToAgeString(dob);
Console.WriteLine(s);
Console.ReadLine();
}
}
}
Hamid KhanPosted Aug 9, 2020, 2:33 PM
Zainal AbidinPosted Mar 6, 2016, 6:23 AM
Zainal AbidinPosted Mar 6, 2016, 6:22 AM
BPosted Oct 26, 2014, 9:20 AM
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Datetmes
{
class Program
{
static void Main()
{
Console.WriteLine("Enter your Date Of Birth As Year Month and Date");
String s = Console.ReadLine();
int[] dob = new int[3];
String[] a = s.Split('.');
for (int i = 0; i < 3; i++)
dob[i] = Int32.Parse(a[i]);
DateTime dt = new DateTime(dob[0],dob[1], dob[2]);
Console.WriteLine("Enter Required Date As Year Month and Date");
String t = Console.ReadLine();
int[] tod = new int[3];
String[] b = t.Split('.');
for (int j = 0; j < 3; j++)
tod[j] = Int32.Parse(b[j]);
DateTime dt1 = new DateTime(tod[0], tod[1], tod[2]);
ShowDifference(dt, dt1);
Console.WriteLine();
Console.ReadLine();
}
static void ShowDifference(DateTime start, DateTime end)
{
// if (start > end)
{
// throw new ArgumentException();
}
// See comment at the end
int years = end.Year - start.Year - 2;
while (start.AddYears(years) <= end)
{
years++;
}
years--;
//Console.WriteLine("{0} years", years);
start = start.AddYears(years);
int months = 0;
while (start.AddMonths(months) <= end)
{
months++;
}
months--;
//Console.WriteLine("{0} months", months);
start = start.AddMonths(months);
int days = 0;
while (start.AddDays(days) <= end)
{
days++;
}
days--;
//Console.WriteLine("{0} days", days);
Console.WriteLine();
//Console.WriteLine();
Console.WriteLine("Your age is: {0} Year(s) {1} Month(s) {2} Day(s) ", years,months,days);
}
}
}
// ABOVE CODE IS WORKING.Enter the date like this 1993.10.22
Iftikar HussainPosted Aug 14, 2013, 7:16 AM
Try like this
public string GetAge(DateTime DateOfBirth)
{
return result;
Regards,
Iftikar
Joxin StanlyPosted Aug 14, 2013, 6:10 AM
to DateTime DOB = new DateTime(year,month,day );
Sanjeeb LenkaPosted Aug 14, 2013, 6:03 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IfExample
{
class DateTimeExtensions
{
public string ToAgeString(DateTime dob)
{
DateTime dt = DateTime.Now;
int days = dt.Day - dob.Day;
if (days < 0)
{
dt = dt.AddMonths(-1);
days += DateTime.DaysInMonth(dt.Year, dt.Month);
}
int months = dt.Month - dob.Month;
if (months < 0)
{
dt = dt.AddYears(-1);
months += 12;
}
int years = dt.Year - dob.Year;
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");
}
static void Main()
{
DateTimeExtensions obj = new DateTimeExtensions();
Console.Write("Enter the Birth of year : ");
int year = int.Parse(Console.ReadLine());
Console.Write("Enter the Birth of month : ");
int month = int.Parse(Console.ReadLine());
Console.Write("Enter the Birth day: ");
int day = int.Parse(Console.ReadLine());
string s = month + "/" + day + "/" + year;
DateTime dob = DateTime.Parse(s);
string s2 = obj.ToAgeString(dob);
Console.WriteLine(s2);
Console.ReadLine();
}
}
}
VijayPosted Aug 14, 2013, 5:47 AM
I tried like this,
i cant get it...
VijayPosted Aug 14, 2013, 5:05 AM
Console.WriteLine("Enter the DOB (dd/mm/yyyy)");
DateTime dob = DateTime.Parse(Console.ReadLine());
I want to enter date is day-month-year format... for that how i want to declare
Sanjeeb LenkaPosted Aug 13, 2013, 4:05 AM
try this code
ex:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IfExample
{
class DateTimeExtensions
{
public string ToAgeString( DateTime dob)
{
DateTime dt = DateTime.Now;
int days = dt.Day - dob.Day;
if (days < 0)
{
dt = dt.AddMonths(-1);
days += DateTime.DaysInMonth(dt.Year, dt.Month);
}
int months = dt.Month - dob.Month;
if (months < 0)
{
dt = dt.AddYears(-1);
months += 12;
}
int years = dt.Year - dob.Year;
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");
}
static void Main()
{
DateTimeExtensions obj = new DateTimeExtensions();
Console.WriteLine("Enter your date of birth in mm/dd/yyyy format to calculate age");
DateTime dob = DateTime.Parse(Console.ReadLine());
string s = obj.ToAgeString(dob);
Console.WriteLine(s);
Console.ReadLine();
}
}
}