从计算日期年龄文本框在C# [英] Calculate age from date in textbox in C#

查看:225
本文介绍了从计算日期年龄文本框在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  

可能重复:结果
  计算从生日年龄


如何计算年龄年,采取从文本框输入格式为 DD / MM / YYYY

例如


  

输入:txtDOB.Text 20/02/1989(字符串格式)结果
  输出:txtAge.Text 23



解决方案

您可以使用的。减去方法的DateTime 链接),然后使用属性来确定实际年龄:

 现在的DateTime = DateTime.Now;
日期时间givenDate = DateTime.Parse(输入);INT天= now.Subtract(givenDate).Days
INT年龄= Math.Floor(天/ 365.24219)

Possible Duplicate:
Calculating age from birthday

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

e.g.

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

解决方案

You can use the Substract method of DateTime (link) and then use the Days property to determine the actual age:

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

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

这篇关于从计算日期年龄文本框在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆