Age in years from DateTime (Date of birth)
Duplicate
How Can I calculate Someone's Age in C#?
I have a datetime
variable that represents the date of birth of a user.
How can I get the age in years from this?
Update I want a precise birthday, so 30.45 years or something.
尝试以下(假设出生日期存储在dtDOB
):
public int getAgeInYears {
TimeSpan tsAge = DateTime.Now.Subtract(dtDOB);
return new DateTime(tsAge.Ticks).Year - 1;
}
从Jeff的问题的答案中被盗:
DateTime now = DateTime.Now;
int age = now.Year - bday.Year;
if (now < bday.AddYears(age)) age--;
You can try with (in Vb):
Dim dateOfBirth As Date
Now.Subtract(dateOfBirth).TotalDays 365
is an Integer division in Vb, I do not know if it has a correspondant in C#.
链接地址: http://www.djcxy.com/p/54352.html上一篇: 简单年龄计算器基于C#中的出生日期
下一篇: 来自DateTime的年龄(出生日期)