Click here to Skip to main content
15,886,578 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I am using a calendar extender on a textbox and i want to calculate age from the selected date and as soon as the date is selected, calculated age should be shown in another textbox. How it can be done? Another problem is OnClientDateSelectionChanged event of calendar extender is not working on which i tried to call a method. Please help.
Posted
Updated 12-Mar-21 13:35pm
v2
Comments
adriancs 12-Mar-21 19:34pm    
https://letmegooglethat.com/?q=c%23+age

C#
int now = int.Parse(DateTime.Today.ToString("yyyyMMdd"));
int dob = int.Parse(dateDOB.ToString("yyyyMMdd"));
string dif = (now - dob).ToString();
string age = "0";
if (dif.Length > 4)
    age = dif.Substring(0, dif.Length - 4);



Thanks,
Ambesha
 
Share this answer
 
C#
DateTime d1=DateTime.Now ;
DateTime d2 = new DateTime(1978,4,5);
TimeSpan difference = d1.Subtract(d2);
difference.TotalDays/365.25).ToString();

or
C#
//call function
int age= Age(DateTime.Parse("4/26/1973"));
//function
public static int Age(DateTime birthDate)
{
int years, months, days;
DateDiff(DateTime.Now, birthDate, out years, out months, out days);
return years;
}

Happy Coding!
:)
 
Share this answer
 
Comments
[no name] 26-Oct-12 6:12am    
my 5
Aarti Meswania 26-Oct-12 6:13am    
thank you! :)
public void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
txt_Birth.Text = Convert.ToString(monthCalendar1.SelectionStart.Date);
int s = monthCalendar1.SelectionStart.Year;
DateTime date = DateTime.Now;
int cur_year = Convert.ToInt32(date.Year);
age = cur_year - s;
txt_age.Text = Convert.ToString(age);
}
 
Share this answer
 
Comments
Mohammed Ahmed Ansari 1-Mar-22 4:42am    
Where are you calling this method?
C#
public static int GetPersonAge(DateTime birthdate, DateTime AgeOnThisDate)
{
    int age = AgeOnThisDate.Year - birthdate.Year;
    if (AgeOnThisDate.Month < birthdate.Month || (AgeOnThisDate.Month == birthdate.Month && AgeOnThisDate.Day < birthdate.Day))
        age--;
    if (age < 0)
        return 0;
    else
        return age;
}
 
Share this answer
 
public string calculateAge( int currentyear, int currentmonth, int currentday, int birthYear, int birthMonth, int birthDay )
{
    int age = 0;
    if ( currentmonth > birthMonth )
        age = currentyear - birthYear;
    else if ( currentmonth < birthMonth )
        age = currentyear - birthYear - 1;
    else // month == birthMonth
    {
        if ( currentday < birthDay )
            age = currentyear - birthYear - 1;
        else
            age = currentyear - birthYear;
    }

    return age.ToString();
} // END calculateAge
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900