Code53计算用户距当前时间的年龄

来源:互联网 发布:婚礼软件哪个好 编辑:程序博客网 时间:2024/05/23 19:30

下面方法计算用户距当前时间(今天)的年龄。这个方法是用C#编写的。这个方法返回整数值 - 用户的年龄。

public static int GetAgeOfUser(DateTime DOB) {

    DateTimetodaysDateTime = 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--;

    }

    returnnoOfYears;

}