可能重复:
如何计算C#中某人的年龄?
我正在尝试创建一个表单,其中输入了客户的出生日期,并且他的年龄会自动显示在txtAge中:
private void Form3_Load(object sender, EventArgs e)
{
dtpDob.Value = DateTime.Today.Date;
SqlConnection conn = new SqlConnection();
}
private void dtpDOB_Leave(object sender, System.EventArgs e)
{
System.DateTime dob = default(System.DateTime);
dob = dtpDob.Value.Date;
txtAge.Text = DateTime.DateDiff(DateInterval.Year, dob, DateTime.Today.Date);
}
Run Code Online (Sandbox Code Playgroud)
但我得到这些错误:
'System.DateTime'不包含'DateDiff'的定义.
当前上下文中不存在名称"DateInterval".
public static int GetAge(DateTime birthDate)
{
return (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.242199);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1473 次 |
| 最近记录: |