C# - convert 'days old' to date of birth

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sskofid11
    New Member
    • Jul 2017
    • 5

    C# - convert 'days old' to date of birth

    (C#)

    Hi,

    I need to convert how many 'days Old' someone is (e.g. 3 years old = 1,095 days old) to a 'date of birth' answer i.e. dd/MM/yyyy. I have tried many different ways how to do this, but to no avail (yet) - e.g. using a mathematical formula but it gets a bit complicated, especially when considering how to implement that into C# (for a beginner, at least).

    What is in the box below is one of the many ways I have tried thus far. I know it's wrong but it's one of many of my attempts. What is the simplest way to go about this (assuming 1 year is 365 days, if the computer doesn't have it's own automated calculation to include leap years)?

    Code:
    public static string GetBirthDate(int daysOld)
            {
                var dateToday = Convert.ToInt32(DateTime.Now);
     
                var calc = (dateToday - 365.25 * daysOld);
     
                return Convert.ToString(calc);
            }
    Finally, if I am to include leap years, would the adjustment to simply be to take into consideration that 1 year = 365.25 years? Or would there be more complexity to this?

    Kind regards.
  • Luk3r
    Contributor
    • Jan 2014
    • 300

    #2
    This would be the easiest way in my opinion:
    Code:
    DateTime birthDate = DateTime.Now.AddDays(-1095);

    Comment

    • sskofid11
      New Member
      • Jul 2017
      • 5

      #3
      this doesn't seem to work

      Comment

      Working...