1

I'm trying to call this function which should return the age of 57 but is returning 58 if I run it today October 18, 2016.

    DateTime myDate3test = Convert.ToDateTime("1958-10-17 13:45:59.473");
    Console.WriteLine(CalculateAge(myDate3test)); //this should return 57 if run today October 18, 2016 since the person is not yet 58


    public static string CalculateAge(DateTime dtDateOfBirth)
    {
        int age = 0;
        DateTime dtNow = DateTime.Now;
        string measurement = string.Empty;

        if (DateTime.Compare(dtNow, dtDateOfBirth) == 1)
        {
            TimeSpan tsAge = dtNow.Subtract(dtDateOfBirth);
            DateTime dtAge = new DateTime(tsAge.Ticks);



            var vNowDate = Convert.ToInt32(dtNow.ToString("yyyyMMdd"));
            var vBirthdate = Convert.ToInt32(dtDateOfBirth.ToString("yyyyMMdd"));
            double diff = (vNowDate - vBirthdate) / 10000;
            age = Convert.ToInt32(Math.Truncate(diff));

            measurement = " year";

            if (age == 0) // patient is not 1 year old yet
            {
                age = dtAge.Month - 1;
                measurement = " month";

                if (age == 0) // patient is not 1 month old yet
                {
                    age = dtAge.Day - 1;
                    measurement = " day";
                }
            }
            if (age > 1)
            {
                measurement += "s";
            }
        }
        else
        {
            // Future date!!!
            measurement = " Unable to calculate age";
            age = -1;
        }

        return age.ToString() + measurement;
    }

Any help is appreciated.

TuSabesTuSabes
  • 61
  • 1
  • 10
  • 2
    [How do I calculate someone's age in C#?](http://stackoverflow.com/q/9/447156) – Soner Gönül Oct 18 '16 at 17:21
  • Hi Soner. I tried the link you mentioned but the code in there is also returning 58 when passing the data that I'm passing DateTime myDate3test = Convert.ToDateTime("1958-10-17 13:45:59.473"); – TuSabesTuSabes Oct 18 '16 at 17:24
  • http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c http://stackoverflow.com/questions/2194999/how-to-calculate-an-age-based-on-a-birthday http://stackoverflow.com/questions/3054715/c-sharp-calculate-accurate-age – Rafael Marques Oct 18 '16 at 17:30
  • Perhaps this might be of some help to you. http://stackoverflow.com/questions/9/how-do-i-calculate-someones-age-in-c – Amey Kamat Oct 18 '16 at 17:31
  • Thanks for the replies Amey, Rafael, tried all those links and I still get an answer of 58 instead of 57 when passing this datetime 1958-10-17 13:45:59.473 – TuSabesTuSabes Oct 18 '16 at 17:35
  • Time is not relevant when calculating age. Only date is relevant, and that question has an answer already. The only reason to consider time is to decide by which time zone you pick the "today" date. If you have some other business requirements that involve time of day in the calculation, you need to be explicit about what those requirements are. For example, "how many years old is a person?" is different than "how many seconds old is a person?" The latter actually requires time and time zone of birth. See also http://codeofmatt.com/2014/04/09/handling-birthdays-and-other-anniversaries/ – Matt Johnson-Pint Oct 18 '16 at 22:39

1 Answers1

2

Your Input is 1958-10-17 so the Answer 58 Years is Correct. http://www.calculator.net/age-calculator.html?today=10%2F17%2F1958&ageat=10%2F18%2F2016&x=74&y=7

M Imtiaz
  • 97
  • 1
  • 2
  • 6
  • Hi M Imtiaz thanks for the reply. Actually the person is still 57 if taking into account the time span of the date 1958-10-17 13:45:59.473, we are trying to write a function to calculate that since that is part of a business requirement – TuSabesTuSabes Oct 18 '16 at 17:30
  • 1
    @TuSabesTuSabes Actually, the person is still 58. That is not how time works.. If you want the person to be 57, add a day. – Cole9350 Oct 18 '16 at 18:45
  • you can make seperate logic for time. first calculate age then perform time comparison and deduct one day if time is not reached yet – M Imtiaz Oct 19 '16 at 08:31