0

I'm working on a simple program that calculates how old a member is and applies the correct membership fee based on age.

        public static DateTime DateTime()
    {
        DateTime birth = new System.DateTime(1991, 9, 20);
        DateTime today = new System.DateTime(2017, 1, 22);
        TimeSpan age = today - birth;
        Console.WriteLine(age);
        return birth;
    }

Here is the code I'm using, my return value is

9256.00:00:00

How can parse this into a more readable yyyy,mm,dd format?

Luke Shinn
  • 346
  • 5
  • 12

2 Answers2

0

As Jon Skeet has put it:

TimeSpan has no sensible concept of "years" because it depends on the start and end point.

Why don't you try his Noda Time instead?

You can manually download here

You can also divide by 365.25 (average number of days in a year), and get the whole number

Convert.ToInt32(age.Days / 365.25);
Community
  • 1
  • 1
Malcolm Salvador
  • 1,476
  • 2
  • 21
  • 40
0

Using nodatime

var ld1 = new LocalDate(1991, 9, 20);
var ld2 = new LocalDate(2017, 1, 22);
var period = Period.Between(ld1, ld2);

Debug.WriteLine(period.Years);
Debug.WriteLine(period.Months);
Debug.WriteLine(period.Days);
Daniel
  • 2,744
  • 1
  • 31
  • 41