Formular una preguntaFormular una pregunta
 

RespondidaCalculate a person's age

Respuestas

  • miércoles, 04 de noviembre de 2009 15:13David M MortonMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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
  • jueves, 05 de noviembre de 2009 4:52Tamer OzMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     RespondidaTiene código
    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);
                }
            }
        }
    

Todas las respuestas

  • miércoles, 04 de noviembre de 2009 15:04OmegaManMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    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)
  • miércoles, 04 de noviembre de 2009 15:13David M MortonMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida

    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
  • miércoles, 04 de noviembre de 2009 15:17OmegaManMVP, ModeradorMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    David is right that timespan doesn't give years...but that is a simple calculation Days/365. :-)
    William Wegerson (www.OmegaCoder.Com)
  • miércoles, 04 de noviembre de 2009 15:18ScottyDoesKnow Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Don't forget leap years Omega. The DateTime method is a lot easier if you want to be accurate.
  • jueves, 05 de noviembre de 2009 4:52Tamer OzMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     RespondidaTiene código
    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);
                }
            }
        }