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

后端 未结 30 2164
名媛妹妹
名媛妹妹 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:51

    An easy to understand and simple solution.

    // Save today's date.
    var today = DateTime.Today;
    
    // Calculate the age.
    var age = today.Year - birthdate.Year;
    
    // Go back to the year in which the person was born in case of a leap year
    if (birthdate.Date > today.AddYears(-age)) age--;
    

    However, this assumes you are looking for the western idea of the age and not using East Asian reckoning.

    0 讨论(0)
  • 2020-11-21 05:52

    The best way that I know of because of leap years and everything is:

    DateTime birthDate = new DateTime(2000,3,1);
    int age = (int)Math.Floor((DateTime.Now - birthDate).TotalDays / 365.25D);
    
    0 讨论(0)
  • 2020-11-21 05:52

    How about this solution?

    static string CalcAge(DateTime birthDay)
    {
        DateTime currentDate = DateTime.Now;         
        int approximateAge = currentDate.Year - birthDay.Year;
        int daysToNextBirthDay = (birthDay.Month * 30 + birthDay.Day) - 
            (currentDate.Month * 30 + currentDate.Day) ;
    
        if (approximateAge == 0 || approximateAge == 1)
        {                
            int month =  Math.Abs(daysToNextBirthDay / 30);
            int days = Math.Abs(daysToNextBirthDay % 30);
    
            if (month == 0)
                return "Your age is: " + daysToNextBirthDay + " days";
    
            return "Your age is: " + month + " months and " + days + " days"; ;
        }
    
        if (daysToNextBirthDay > 0)
            return "Your age is: " + --approximateAge + " Years";
    
        return "Your age is: " + approximateAge + " Years"; ;
    }
    
    0 讨论(0)
  • 2020-11-21 05:53

    Another function, not by me but found on the web and refined it a bit:

    public static int GetAge(DateTime birthDate)
    {
        DateTime n = DateTime.Now; // To avoid a race condition around midnight
        int age = n.Year - birthDate.Year;
    
        if (n.Month < birthDate.Month || (n.Month == birthDate.Month && n.Day < birthDate.Day))
            age--;
    
        return age;
    }
    

    Just two things that come into my mind: What about people from countries that do not use the Gregorian calendar? DateTime.Now is in the server-specific culture I think. I have absolutely zero knowledge about actually working with Asian calendars and I do not know if there is an easy way to convert dates between calendars, but just in case you're wondering about those Chinese guys from the year 4660 :-)

    0 讨论(0)
  • 2020-11-21 05:53

    Keeping it simple (and possibly stupid:)).

    DateTime birth = new DateTime(1975, 09, 27, 01, 00, 00, 00);
    TimeSpan ts = DateTime.Now - birth;
    Console.WriteLine("You are approximately " + ts.TotalSeconds.ToString() + " seconds old.");
    
    0 讨论(0)
  • 2020-11-21 05:53

    This is one of the most accurate answers that is able to resolve the birthday of 29th of Feb compared to any year of 28th Feb.

    public int GetAge(DateTime birthDate)
    {
        int age = DateTime.Now.Year - birthDate.Year;
    
        if (birthDate.DayOfYear > DateTime.Now.DayOfYear)
            age--;
    
        return age;
    }
    
    
    
    
    
    0 讨论(0)
提交回复
热议问题