下面方法计算用户距当前时间(今天)的年龄。这个方法是用 C# 编写的。这个方法返回整数值 - 用户的年龄。
public static int GetAgeOfUser(DateTime DOB) {
DateTime todaysDateTime = DateTime.Today; //Get Today DateTime
int noOfYears = todaysDateTime.Year - DOB.Year;
if (DateTime.Now.Month < DOB.Month || (DateTime.Now.Month == DOB.Month
&& DateTime.Now.Day < DOB.Day)){
noOfYears--;
}
return noOfYears;
}