In C#, how do I calculate someone's age based on a DateTime type birthday?

后端 未结 30 2161
名媛妹妹
名媛妹妹 2020-11-21 05:14

Given a DateTime representing a person\'s birthday, how do I calculate their age in years?

30条回答
  •  时光说笑
    2020-11-21 05:45

    I have a customized method to calculate age, plus a bonus validation message just in case it helps:

    public void GetAge(DateTime dob, DateTime now, out int years, out int months, out int days)
    {
        years = 0;
        months = 0;
        days = 0;
    
        DateTime tmpdob = new DateTime(dob.Year, dob.Month, 1);
        DateTime tmpnow = new DateTime(now.Year, now.Month, 1);
    
        while (tmpdob.AddYears(years).AddMonths(months) < tmpnow)
        {
            months++;
            if (months > 12)
            {
                years++;
                months = months - 12;
            }
        }
    
        if (now.Day >= dob.Day)
            days = days + now.Day - dob.Day;
        else
        {
            months--;
            if (months < 0)
            {
                years--;
                months = months + 12;
            }
            days += DateTime.DaysInMonth(now.AddMonths(-1).Year, now.AddMonths(-1).Month) + now.Day - dob.Day;
        }
    
        if (DateTime.IsLeapYear(dob.Year) && dob.Month == 2 && dob.Day == 29 && now >= new DateTime(now.Year, 3, 1))
            days++;
    
    }   
    
    private string ValidateDate(DateTime dob) //This method will validate the date
    {
        int Years = 0; int Months = 0; int Days = 0;
    
        GetAge(dob, DateTime.Now, out Years, out Months, out Days);
    
        if (Years < 18)
            message =  Years + " is too young. Please try again on your 18th birthday.";
        else if (Years >= 65)
            message = Years + " is too old. Date of Birth must not be 65 or older.";
        else
            return null; //Denotes validation passed
    }
    

    Method call here and pass out datetime value (MM/dd/yyyy if server set to USA locale). Replace this with anything a messagebox or any container to display:

    DateTime dob = DateTime.Parse("03/10/1982");  
    
    string message = ValidateDate(dob);
    
    lbldatemessage.Visible = !StringIsNullOrWhitespace(message);
    lbldatemessage.Text = message ?? ""; //Ternary if message is null then default to empty string
    

    Remember you can format the message any way you like.

提交回复
热议问题