此方法仅用于简单计算年龄,不适用于计算虚岁
public static int GetAge(DateTime birthDate)
{
DateTime now = DateTime.Now;
int age = now.Year - birthDate.Year;
if (birthDate> now.AddYears(-age)) age--;
return age;
}
如果时间不分时区,DateTime.Now可以改为DateTime.UtcNow。