5

I need to output the difference between 2 days in years, months and days.

With timespan and subtract i only get the days but is it possible to output in years, months and days.

This is what i have so far:

public string Leeftijdsverschil(DateTime p1, DateTime p2)
{
    if (p1 > p2)
    {
        DateTime verschil = p1.Subtract(TimeSpan.FromTicks(p2.Ticks));
        return verschil.ToString();
    }
    else
    {
        DateTime verschil = p2.Subtract(TimeSpan.FromTicks(p1.Ticks));
        return verschil.ToString();
    }
}
Andrey Korneyev
  • 26,353
  • 15
  • 70
  • 71
Kris
  • 97
  • 3
  • 15

2 Answers2

13

The .NET types don't give you a decent answer to this without having to write code to try one answer and adjust appropriately, but my Noda Time project is designed to handle this sort of thing. For example:

LocalDate x = new LocalDate(2013, 5, 21);
LocalDate y = new LocalDate(2014, 12, 15);
Period p = Period.Between(x, y);
Console.WriteLine("{0} {1} {2}", p.Years, p.Months, p.Days);

Result:

1 6 24

(i.e. 1 year, 6 months and 24 days)

If you decide to use Noda Time for this, you could just do it within your method, using DateTime elsewhere and converting between the types... but it would generally be better to embrace Noda Time more widely. The aim is for it to allow your code to be clearer than just using DateTime.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

To get the delta, just subtract directly:

var delta = p1 - p2;

This gives you a TimeSpan. From there, you have access to .Days. Months and years, however, are much more complicated. If you want the difference in those terms, you'll have to compare the properties of p1 and p2 directly (or: as Jon says: use Noda Time)

Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
  • Not possible, due to leap years, not to mentiond months. Imagine if your birthday was on Feb 29. You'd have to do the full date arithmetic in any case – Panagiotis Kanavos Dec 15 '14 at 09:34
  • 1
    @PanagiotisKanavos please tell me very explicitly which bit is "not possible". Subtracting the dates is certainly possible. As I noted; getting the year / month delta is more complicated, but is still absolutely possible. – Marc Gravell Dec 15 '14 at 09:35
  • Getting this kind of difference going through a TimeSpan is impossible, if you cross even a single month threshold. You need to compare days, months, years in every case. – Panagiotis Kanavos Dec 15 '14 at 09:36
  • @PanagiotisKanavos exactly; which is why I said "Months and years, however, are much more complicated. If you want the difference in those terms, you'll have to compare the properties of p1 and p2 directly" – Marc Gravell Dec 15 '14 at 09:38
  • Didn't argue with that, it's the TimeSpan and why a simple difference isn't enough. It'a a different kind of arithmetic - usually taught in elementary school. Different bases and carryovers for each element of the value. I still remember how I failed trying to calculate my age in 4th grade, by trying to convert years to minutes $(. – Panagiotis Kanavos Dec 15 '14 at 09:42