Click here to Skip to main content
15,860,844 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi I have a problem I need the age to be in Years(int)
Here is my piece of code

What I have tried:

C#
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);


}
Posted
Updated 17-Feb-17 3:50am
Comments
Hasham Ahmad 11-Jan-17 1:19am    
Just a thought.. why cant you give a dropdown to ask the user to select their age instead? otherwise its a simple calculation, just parse the date from Calendar1.SelectedDate.ToString() and calculate it with the current date, then bind it with txtDob.Text.
Member 12605293 11-Jan-17 3:52am    
Hi Hasham Thanks for your idea.And I hv tried the below one but it is showing 1year 1month when I select 11 dec 2016

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 + " years" + months + " months");
txtAge.Text = ageval;
}
Jon McKee 11-Jan-17 1:26am    
You have already posted this here. Either update your original question or post a response to an answer already given. You have given little effort other than copying what a response gave you. Please attempt the solution on your own and post relevant code - we are not here to do your job for you.
Member 12605293 11-Jan-17 3:59am    
Hi Jon
I am New to developing as well as in this page sorry the cause.

If you want to calculate age in years, try and adapt from this:
using System;

public class Program
{
	public static void Main()
	{
		DateTime dob = new DateTime(2000, 1, 11); // Calendar1.SelectedDate
		DateTime today = DateTime.Today;
		int age = today.Year - dob.Year;
		Console.WriteLine("Age in years " + age);
	}
}
Learn DateTime Structure (System)[^]
 
Share this answer
 
v2
Comments
Jon McKee 11-Jan-17 1:56am    
Too kind +5, though I do understand the drive to help :)
Peter Leow 11-Jan-17 2:09am    
Thank you, Jon.
Richard Deeming 17-Feb-17 12:55pm    
That's not quite right. What if dob is 2016-12-31 and today is 2017-01-01? :)
Peter Leow 17-Feb-17 20:37pm    
It is intended to be the clue for OP to find the way to the destination.
To Display Calender and Calcualte the Age in a Text box , The Fully Code is below, Please Check it Out:

using System;

namespace CalenderConcepts
{
public partial class Calender : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
txtDBO.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 + "years" + months + "months");
TextBox1.Text = ageval;
Calendar1.Visible = true;
}
}
}



Thanks,
Abhishek
 
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