Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have tried the below code and dont know where I have to pass this with value because the value is filled by the user.

What I have tried:

C#
private static int CalculateAge(DateTime DOB)
{
    int age = 0;
    age = DateTime.Now.Year - DOB.Year;

    if (DateTime.Now.DayOfYear < DOB.DayOfYear)
      age = age - 1;

    return age;
}

protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
    txtbox2.Text = Calendar1.SelectedDate.ToString();
    //int CalculateAge()

    Calendar1.Visible = false;
    txtAge.Text= CalculateAge()
}
Posted
Updated 10-Jan-17 5:28am
v3
Comments
Tomas Takac 10-Jan-17 6:24am    
What is stopping you from passing the value to the method?
Member 12605293 10-Jan-17 6:30am    
Hi tom
Don't know where to pass whether in code behind or in aspx page
Member 12605293 10-Jan-17 6:29am    
Hi
Please provide a code for it Im not getting you

I'm not quite sure, if your
Calendar1.SelectedDate
is already a DateTime.

If so, you should pass this as argument into your function. If not, you have to parse the value to DateTime and then pass it.
 
Share this answer
 
what i understand that you are not sure what is the event, you should handle to reflect the age immediately after typing the age not after lost focus. if this is a desktop application, there should have been some on change event or keypress event. But you must check the date is valid before calculating the Age.

Hope this would help you.
 
Share this answer
 
Use DateTime.Parse or DateTime.ParseExact to convert your Calendar1.SelectedDate.ToString() to a DateTime object and just pass this object to the CalculateAge(..) method. That's all.

Sample:
Use it inside Calendar1_SelectionChanged1(...),
C#
DateTime txtMyDate = 
  DateTime.ParseExact(txtbox2.Text, "M/d/yyyy", CultureInfo.InvariantCulture);
Also can use DateTime.TryParseExact.
 
Share this answer
 
v3
Comments
Member 12605293 11-Jan-17 0:17am    
Hi See this is my piece of code ,and all I need is to get a AGE in TxtAge.txt texbox like 50 ,51.How can I achieve this any pulgin needed?

protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
txtDob.Text = Calendar1.SelectedDate.ToString();
Calendar1.Visible = false;

DateTime txtMyDate = DateTime.ParseExact(txtAge.Text, "M/d/yyyy", CultureInfo.InvariantCulture);


}
Member 12605293 18-Jan-17 2:54am    
Hi
I have tried in this way but the result is showing me to conversion failure int to string

My Code
protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
txtDob.Text = Calendar1.SelectedDate.ToString();
Calendar1.Visible = false;
int year = DateTime.Now.Year - Calendar1.SelectedDate.Year;
int months = DateTime.Now.Month - Calendar1.SelectedDate.Month;

string ageval = ((year - 1) + " years" + (-12 - months) + " months");

if (months > 12)
{
year = year + 1;
}
int value = int.Parse(ageval);
value = (ToString(txtAge.Text)) ;

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