1

I'm currently working on a school project for which I've met the requirements, but I want to challenge myself. How can I accurately display my age in the following format based on today's date/time?

Age: 27 Years -or- Days -or- Hours -or- Seconds (Accounting for Leap Year)

Research I did: How would you calculate the age in C# using date of birth (considering leap years)

I'm more so looking for the math behind it. Here's the math I'm currently using but it's only accurate to 16hr or 960 minutes or 57,600 seconds.

// Tried using "double" datatype, still same problem.
int years = DateTime.Now.Year - dateBirthDate.Year;
int days = (years / 4) + (years * 365);
int hours = (days * 24) + DateTime.Now.Hour;
int minutes = hours * 60;
int seconds = (minutes * 60) + ((DateTime.Now.Hour * 60) * 60) + DateTime.Now.Second;

Should be showing close to 0.

Output:

Thank you Mat, what is your date of birth? Feel free to include the time you were born. 09/08/2018 5:11pm
Years   :0
Days    :0
Minutes :1020
Seconds :122425

#UPDATE #1

I've managed to get the code partially working but have discovered another unforseen issue. Now it will not account for birthdays that have yet to come. Thoughts?

//Needed casting so I could remove the decimals.
TimeSpan span = DateTime.Now.Subtract(dateBirthDate);
int years = (int)span.Days/365;
int months = years * 12;
int days = (int)span.TotalDays;
int hours = (int)span.TotalHours;
int minutes = (int)span.TotalMinutes;
int seconds = (int)span.TotalSeconds;
user5664615
  • 400
  • 1
  • 10
  • 17
  • I feel like my math accounting for leap year isn't a trustworthy method either. Suppose we do land on a leap year then the math would be inaccurate as well. –  Sep 08 '18 at 21:54
  • Here's a fun read through http://yourcalendricalfallacyis.com/ – clarkitect Sep 08 '18 at 22:36
  • You are going to want to create a unit test for this that takes a pair of dates and does the calculations. Do a separate calculation using something like Excel to figure out the expected results for each date pair. You need to take into account people born on February 29th (or doing the calculation on Feb 29). You also need to consider pairs of dates that span the end of Feb, 2100 (which is not a leap year). Doing this kind of date calculation taxes the mind. When you finish, write an algorithm that figure out when Easter occurs each year! – Flydog57 Sep 09 '18 at 00:29
  • @Flydog57 I secretly hate you right now, I thought for sure I had it but I 100% forgot about what happens ON leap year birthdays.... –  Sep 09 '18 at 00:48

1 Answers1

1

Wonky Workaround


Had to cast TimeSpan to a int to remove the decimals. In order to grab the TimeSpan year I simply took the months and divided by 365 then cast it to an (int) so that it would only display the whole number. Then I created an if/else conditional and a nested one to adjust for birthdays that are currently happening or have yet to come. Logic seems sound.

        //Needed casting so I could remove the decimals.
        TimeSpan span = DateTime.Now.Subtract(dateBirthDate);

        //Creating workable if/else to account for birthday's yet to come.
        int dateCorrectorMonthNow = DateTime.Now.Month;
        int dateCorrectorDayNow = DateTime.Now.Day;
        int dateCorrectorMonthThen = dateBirthDate.Month;
        int dateCorrectorDayThen = dateBirthDate.Day;


        int years = (int)span.Days/365;
        int months = years * 12;
        int days = (int)span.TotalDays;
        int hours = (int)span.TotalHours;
        int minutes = (int)span.TotalMinutes;
        int seconds = (int)span.TotalSeconds;

        if (dateCorrectorMonthNow <= dateCorrectorMonthThen)
        {
            if (dateCorrectorDayNow  <= dateCorrectorDayThen)
            {
                Console.WriteLine($"Years   :{years}");
            }
            else
            {
                Console.WriteLine($"Years   :{years-1}");
            }
        }
        else
        {
            Console.WriteLine($"Years   :{years}");
        }
Community
  • 1
  • 1