Click here to Skip to main content
15,879,096 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have one datetime picker and one textbox.

I want that when i select date from datetime picker the age will automatically calculate acording to date which i select from datetime picker and the age is display in that textbox.
Posted
Updated 9-Feb-15 23:46pm
v3

C#
// dt1 is the date picked by the user
TimeSpan ts  = DateTime.Now - dt1;

https://msdn.microsoft.com/en-us/library/system.timespan.aspx[^]
 
Share this answer
 
Try this sample and adapt it to your form:
using System;
public class Program
{
	public static void Main()
	{
		//DateTime dob = dateTimePicker1.Value; // assuming dateTimePicker1 is used
		DateTime dob = new DateTime(1995, 2, 10);
		DateTime today = DateTime.Today;
		int age = today.Year - dob.Year;
		if (dob.AddYears(age) > today)
		{
			age--;
		}
		Console.WriteLine(age);
	}
}

Refer: DateTimePicker.Value Property[^]
 
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