2

So I'm working on this little app that has functions for dates (show difference in days, months, years etc) Now doing things like "The selected date was 14151 days, 465 months and 39 years ago" is easy, but how can I get it to say it in a readable format ("15 days, 7 months and 39 years ago")?

Right now to do that I've got it set to get the difference in years like

int years = selectedDate.Year - currentDate.Year

same for months and days. However this barely works and goes wrong especially when going a month forward (when it might say 15 days and 1 month because date1.Day - date2.Day = 15, while it might actually be, say 8 days. I think this is because of the way dates work with months n stuff but I'm not sure.

Anyway, I hope i made any sense because I can't really keep track myself even. Any ideas are much appreciated. :D

Syed Farjad Zia Zaidi
  • 3,302
  • 4
  • 27
  • 50
Nisse
  • 47
  • 1
  • 7
  • See http://stackoverflow.com/questions/842057/how-do-i-convert-a-timespan-to-a-formatted-string – DShook May 16 '14 at 18:25
  • Consider using [Noda Time](http://nodatime.org), specifically `Period.Between`. See [my answer to a similar question](http://stackoverflow.com/a/22802697/634824). – Matt Johnson-Pint May 16 '14 at 18:51
  • He is asking about differences involving years and months. This question is definitely not a duplicate of http://stackoverflow.com/q/842057/1442776 – SoftwareFactor May 16 '14 at 18:57

6 Answers6

5

Subtracting one DateTime from another returns a TimeSpan which you can use to determine the period of time between the two dates, expressed as a time measurement of days, hours, minutes, seconds, etc.

TimeSpan result = selectedDate - currentDate;
result.Days...
result.Hours...
etc.

Getting months and years is trickier because the days in a month varies per year etc. You'd have to do that math manually, perhaps off of the original DateTime objects.

Haney
  • 32,775
  • 8
  • 59
  • 68
  • Why can't this return a simple DateTime back? I don't understand why Microsoft made a intermediate class when we could just use a simple Time class. – Shadowblitz16 Sep 10 '21 at 00:46
2

With short periods of time you could do it like this:

        TimeSpan timeDiff = selectedDate - currentDate;
        string readableDiff = string.Format(
            "{0:D2} hrs, {1:D2} mins, {2:D2} secs",
            timeDiff.Hours, timeDiff.Minutes, timeDiff.Seconds);

But I noticed that you are dealing with longer periods involving years and months. For calculations like this you need something more powerful than a TimeSpan. You can check out Jon Skeet's Noda Time port for .NET platform: https://code.google.com/p/noda-time/

With Noda Time you could do something like this:

        var period = Period.Between(selectedDate, currentDate,
            PeriodUnits.Years | PeriodUnits.Months | PeriodUnits.Days);
        string readableDifference = string.Format(
            "{0} years, {1} months and {2} days",
            period.Years, period.Months, period.Days);
SoftwareFactor
  • 8,430
  • 3
  • 30
  • 34
0

You can do math with the DateTime class. The result is a TimeSpan that behaves similarly to a DateTime, just representing a length of time instead of an absolute date.

DateTime selectedDate;
DateTime currentDate;

TimeSpan difference = selectedDate.Subtract(currentDate);

string result = difference.ToString("whatever format you like");
Ben Bartle
  • 1,080
  • 11
  • 12
0

Use a TimeSpan and Math.Abs to account for negative values:

DateTime date1 = new DateTime(2012, 5, 15);
DateTime date2 = new DateTime(2014, 5, 16);

TimeSpan dateDiff = date1 - date2;

int years = Math.Abs((int)dateDiff.TotalDays) / 365;
CoolBots
  • 4,770
  • 2
  • 16
  • 30
0

I do this to get the difference between two dates in months:

var start = startDate.Value;
var end = endDate.Value;

var duration = ((end.Year - start.Year) * 12) + end.Month - start.Month;

Of course the only reason I get .Value is because they are nullable dates, but I imagine you could use something similar in your situation.

awh112
  • 1,466
  • 4
  • 22
  • 34
0

If you take the difference between two DateTime objects you get a TimeSpan object.

DateTime a = DateTime.Now;
DateTime b = DateTime.Now.AddYears(-1);
TimeSpan c = a - b;
Console.WriteLine( c );

Will result in an answer of 364.23:59:59.9951930. You can either overload the toString() for a formatted response, or get c.TotalMilliseconds and divide/modulos as needed to get how many years/months/etc...

rbucinell
  • 73
  • 1
  • 2
  • 10