Calculate age from date in textbox in C#

Possible Duplicate:
Calculating age from birthday

How do you calculate age in years, taking input from TextBox in the format dd/MM/yyyy ?

eg

input: txtDOB.Text 20/02/1989 (String format)
output: txtAge.Text 23


您可以使用DateTimeSubstract方法(链接),然后使用Days属性来确定实际的年龄:

DateTime now = DateTime.Now;
DateTime givenDate = DateTime.Parse(input);

int days = now.Subtract(givenDate).Days
int age = Math.Floor(days / 365.24219)

TimeSpan TS = DateTime.Now - new DateTime(1989, 02, 20);
double Years = TS.TotalDays / 365.25;  // 365 1/4 days per year

As already noted in a comment, the correct answer is here: Calculate age in C#

You just need to get the birthday as a DateTime:

DateTime bday = DateTime.ParseExact("20/02/1989", "dd/MM/yyyy", CultureInfo.InvariantCulture);
链接地址: http://www.djcxy.com/p/54370.html

上一篇: iOS UIBezierAnimation曲线,如何创建某种弧?

下一篇: 从C#中的文本框中的日期计算年龄