年龄计算

0 .net c# datetime

可能重复:
如何计算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".

Osk*_*lin 6

public static int GetAge(DateTime birthDate)
        {
            return (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.242199);
        }
Run Code Online (Sandbox Code Playgroud)