C# 根据就诊日期计算年龄

C# 根据就诊日期计算年龄,c#,fastreport,C#,Fastreport,我需要编写C代码,根据出生日期和固定的检查日期计算一个人的年龄 目前,代码是这样编写的: public static int CalculateAge(string birthdate) { DateTime dob = DateTime.Parse(birthdate); int age = DateTime.Now.Year - dob.Year; if (DateTime.Now.Month < dob.Month)

我需要编写C代码,根据出生日期和固定的检查日期计算一个人的年龄

目前,代码是这样编写的:

public static int CalculateAge(string birthdate)
{
  DateTime dob = DateTime.Parse(birthdate);

  int age = DateTime.Now.Year - dob.Year;
  if (DateTime.Now.Month < dob.Month)                         
  {
    age--;
  }
  else if (DateTime.Now.Month == dob.Month &&
    DateTime.Now.Day < dob.Day)
  {
    age--;
  }

  return age;
}
publicstaticintcalculateage(字符串生日)
{
DateTime dob=DateTime.Parse(生日);
int age=DateTime.Now.Year-dob.Year;
if(DateTime.Now.Month

我需要传入第二个变量ExamDate来计算年龄。按照目前的编写方式,当FastReport运行时,比如说3年后,当显示报告时,显然现在22岁的人将是25岁。我知道我们不能使用DateTime。现在

将第二个日期作为参数传递,并将所有事件从
DateTime更改。现在
到此日期:

public static int CalculateAge(string birthdate, string examDate)
{
  DateTime dob = DateTime.Parse(birthdate);
  DateTime ed = DateTime.Parse(examDate);

  int age = ed.Year - dob.Year;
  if (ed.Month < dob.Month)                         
  {
    age--;
  }
  else if (ed.Month == dob.Month &&
    ed.Day < dob.Day)
  {
    age--;
  }

  return age;
}
publicstaticintcalculateage(字符串birthdate,字符串examDate)
{
DateTime dob=DateTime.Parse(生日);
DateTime ed=DateTime.Parse(examDate);
int age=编辑年份-出生年份;
如果(月初<月初)
{
年龄--;
}
否则,如果(ed.Month==dob.Month&&
教育日<出生日)
{
年龄--;
}
回归年龄;
}

因此,当你通过个人出生日期和考试日期时,结果总是一样的。

“我知道我们不能使用DateTime.Now”-为什么不?如果没有,那你为什么要用它呢?对不起,我想我们现在可以用DateTime。我不熟悉C#编码。我想说的是,如果将来打开报告,我们不希望员工的年龄发生变化。您不能添加第二个参数吗?所以,
CalculateAge(字符串birthDate,字符串visitDate)
。现在我明白了。您需要类似于
CalculateAge(字符串birthdate,字符串examdate)
Correct的内容。例如,一名患者的出生日期为1992年1月2日,在2015年9月4日就诊。他的年龄是23岁。如果我明年再打开报告,他的年龄将是24岁,这是不正确的,因为他23岁时参加了考试。他的所有后续测试结果明年也会发生变化,我们不希望这种情况发生。