提出问题提出问题
 

已答复Calculate a person's age

答案

  • 2009年11月4日 15:13David M MortonMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    TimeSpan won't do it.  There's no property for Years.

    Here's a couple of ways:

    1. Use a while loop

    int age = 0;
    while (DateTime.Today.AddYears(-(i + 1)) >= birthDay)
        age++;

    2. Use another more detailed method:

    //// get the raw years
    DateTime birthDay = new DateTime(1979, 11, 4);
    var age = DateTime.Today.Year - birthDay.Year;

    // adjust for whether the birthday is past or not.
    DateTime thisYearsBirthDay = new DateTime(DateTime.Today.Year, birthDay.Month, birthDay.Day);
    age -= DateTime.Today >= thisYearsBirthDay ? 0 : 1;


    Console.WriteLine(age);


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • 2009年11月5日 4:52Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
    Hi,

    You can use the following class also.
        class Age
        {
            public Age(DateTime dateOfBirth)
            {
                CalculateAge(dateOfBirth,DateTime.Now);
            }
            public Age(DateTime dateOfBirth, DateTime dateToSubstractFrom)
            {
                CalculateAge(dateOfBirth, dateToSubstractFrom);
            }
            public int Year { get; set; }
            public int Month { get; set; }
            public int Day { get; set; }
            public int Hour { get; set; }
            public int Minute { get; set; }
            public int Second { get; set; }
            private void ClearValue()
            {
                Year = 0;
                Month = 0;
                Day = 0;
                Hour = 0;
                Minute = 0;
                Second = 0;
            }
            private void CalculateAge(DateTime dateOfBirth,DateTime dateToSubstractFrom)
            {
                ClearValue();
                DateTime internalDateOfBirth = dateOfBirth;
                while (internalDateOfBirth.AddYears(1) <= dateToSubstractFrom)
                {
                    this.Year++;
                    internalDateOfBirth = internalDateOfBirth.AddYears(1);
                }
                while (internalDateOfBirth.AddMonths(1) <= dateToSubstractFrom)
                {
                    this.Month++;
                    internalDateOfBirth = internalDateOfBirth.AddMonths(1);
                }
                while (internalDateOfBirth.AddDays(1) <= dateToSubstractFrom)
                {
                    this.Day++;
                    internalDateOfBirth = internalDateOfBirth.AddDays(1);
                }
                while (internalDateOfBirth.AddHours(1) <= dateToSubstractFrom)
                {
                    this.Hour++;
                    internalDateOfBirth = internalDateOfBirth.AddHours(1);
                }
                while (internalDateOfBirth.AddMinutes(1) <= dateToSubstractFrom)
                {
                    this.Minute++;
                    internalDateOfBirth = internalDateOfBirth.AddMinutes(1);
                }
                while (internalDateOfBirth.AddSeconds(1) <= dateToSubstractFrom)
                {
                    this.Second++;
                    internalDateOfBirth = internalDateOfBirth.AddSeconds(1);
                }
            }
        }
    

全部回复

  • 2009年11月4日 15:04OmegaManMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Use the TimeSpan Structure to achieve what you want. You can extract days, minutes or even years. Check out the example in MSDN. GL HTH
    William Wegerson (www.OmegaCoder.Com)
  • 2009年11月4日 15:13David M MortonMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复

    TimeSpan won't do it.  There's no property for Years.

    Here's a couple of ways:

    1. Use a while loop

    int age = 0;
    while (DateTime.Today.AddYears(-(i + 1)) >= birthDay)
        age++;

    2. Use another more detailed method:

    //// get the raw years
    DateTime birthDay = new DateTime(1979, 11, 4);
    var age = DateTime.Today.Year - birthDay.Year;

    // adjust for whether the birthday is past or not.
    DateTime thisYearsBirthDay = new DateTime(DateTime.Today.Year, birthDay.Month, birthDay.Day);
    age -= DateTime.Today >= thisYearsBirthDay ? 0 : 1;


    Console.WriteLine(age);


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
  • 2009年11月4日 15:17OmegaManMVP, 版主用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    David is right that timespan doesn't give years...but that is a simple calculation Days/365. :-)
    William Wegerson (www.OmegaCoder.Com)
  • 2009年11月4日 15:18ScottyDoesKnow 用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     
    Don't forget leap years Omega. The DateTime method is a lot easier if you want to be accurate.
  • 2009年11月5日 4:52Tamer OzMVP用户奖牌用户奖牌用户奖牌用户奖牌用户奖牌
     已答复包含代码
    Hi,

    You can use the following class also.
        class Age
        {
            public Age(DateTime dateOfBirth)
            {
                CalculateAge(dateOfBirth,DateTime.Now);
            }
            public Age(DateTime dateOfBirth, DateTime dateToSubstractFrom)
            {
                CalculateAge(dateOfBirth, dateToSubstractFrom);
            }
            public int Year { get; set; }
            public int Month { get; set; }
            public int Day { get; set; }
            public int Hour { get; set; }
            public int Minute { get; set; }
            public int Second { get; set; }
            private void ClearValue()
            {
                Year = 0;
                Month = 0;
                Day = 0;
                Hour = 0;
                Minute = 0;
                Second = 0;
            }
            private void CalculateAge(DateTime dateOfBirth,DateTime dateToSubstractFrom)
            {
                ClearValue();
                DateTime internalDateOfBirth = dateOfBirth;
                while (internalDateOfBirth.AddYears(1) <= dateToSubstractFrom)
                {
                    this.Year++;
                    internalDateOfBirth = internalDateOfBirth.AddYears(1);
                }
                while (internalDateOfBirth.AddMonths(1) <= dateToSubstractFrom)
                {
                    this.Month++;
                    internalDateOfBirth = internalDateOfBirth.AddMonths(1);
                }
                while (internalDateOfBirth.AddDays(1) <= dateToSubstractFrom)
                {
                    this.Day++;
                    internalDateOfBirth = internalDateOfBirth.AddDays(1);
                }
                while (internalDateOfBirth.AddHours(1) <= dateToSubstractFrom)
                {
                    this.Hour++;
                    internalDateOfBirth = internalDateOfBirth.AddHours(1);
                }
                while (internalDateOfBirth.AddMinutes(1) <= dateToSubstractFrom)
                {
                    this.Minute++;
                    internalDateOfBirth = internalDateOfBirth.AddMinutes(1);
                }
                while (internalDateOfBirth.AddSeconds(1) <= dateToSubstractFrom)
                {
                    this.Second++;
                    internalDateOfBirth = internalDateOfBirth.AddSeconds(1);
                }
            }
        }