0

I was wondering if there was already an implemented Datecounter which will count how many years, month and days it differs from a date to another? And if there is a difference, the function will count how many years, month and days in difference and store it and we only need to use Console.Writeline(timecomparer.yearDiffCounter); to tell them how many years it is in difference

For example (pseudocode, not 100% correct)!

Date date1 = new Date("2013-07-05"); 
Date date2 = new Date("2010-07-05"); 
TimeComparer compare = new TimeComparer(); 

compare.differDate(date1,date2); //here it will count and give 3 years difference
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Khiem-Kim Ho Xuan
  • 1,301
  • 1
  • 22
  • 45

4 Answers4

2

Using Noda Time:

LocalDate date1 = new LocalDate(2013, 07, 05);
LocalDate date2 = new LocalDate(2010, 07, 05);
Period period = Period.Between(date2, date1, PeriodUnits.YearMonthDay);
Console.WriteLine("{0} years, {1} months, {2} days",
                  period.Years, period.Months, period.Days);

// "3 years, 0 months, 0 days"
Timothy Groote
  • 8,614
  • 26
  • 52
Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
  • @Timothy - Why would it be an edit? This is an actual answer to the original question. And its the *only* answer thus far that actually delivers what was asked for, so it does not deserve a down vote. – Matt Johnson-Pint Sep 06 '13 at 12:29
  • What i meant by that is now there are two answers basically telling the same thing, this one just tells you how you would go about it. Sorry bout the edit (nothing really changed) but i needed to do that to fix the downvote. – Timothy Groote Sep 06 '13 at 13:40
  • @Timothy - had I just edited the other answer, it would have given points where none were deserved. I did edit it to update the link, but I typically don't make drastic edits to others questions. Only minor ones. – Matt Johnson-Pint Sep 06 '13 at 14:11
1

Powerful solution for time is Noda Time by Jon Skeet.

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575
Suhan
  • 1,434
  • 2
  • 13
  • 28
1
public  string DateDiff(DateTime DateTime1, DateTime DateTime2)
{
    string dateDiff = null;

    TimeSpan ts1 = new TimeSpan(DateTime1.Ticks);
    TimeSpan ts2 = new TimeSpan(DateTime2.Ticks);
    TimeSpan ts = ts1.Subtract(ts2).Duration();
    dateDiff = ts.Days.ToString() + "day"
        + ts.Hours.ToString() + "hours"
        + ts.Minutes.ToString() + "minutest"
        + ts.Seconds.ToString() + "seconds";

    return dateDiff;
}

my way you can change.

PengWu
  • 65
  • 5
  • 1
    The OP asked for Years, Months, and Days. You can't get that from a TimeSpan. – Matt Johnson-Pint Sep 06 '13 at 01:50
  • You can still get months and days from a timespan. – Timothy Groote Sep 06 '13 at 08:18
  • @TimothyGroote - No, you can't get months either. Months is also not a measure of elapsed time. How many days are in a month? It depends on *which* months you are talking about. Take a look at the [`TimeSpan`](http://msdn.microsoft.com/en-us/library/system.timespan.aspx) object. There is no `Months` or `TotalMonth` property. – Matt Johnson-Pint Sep 06 '13 at 12:34
  • I could have sworn they were there... cognitive dissonance at play maybe? :P – Timothy Groote Sep 06 '13 at 13:41
-1

Boolean operations on DateTime objects in C# yield TimeSpan objects

DateTime Yesterday = DateTime.Now().AddDays(-1);
DateTime Today = DateTime.Now();

TimeSpan difference = Today - Yesterday;

A timespan can then tell you how many days, hours, minutes, seconds etc it has.

If you want years from a Timespan, see this answer by brianary

Community
  • 1
  • 1
Timothy Groote
  • 8,614
  • 26
  • 52
  • 1
    A `TimeSpan` does not have a value for years, because it's not an exact measure of elapsed time. – Matt Johnson-Pint Sep 06 '13 at 01:44
  • So a TimeSpan can't directly tell you how many years it's been, but it doesn't take rocket science to figure out how to calculate how many years it spans. Also, how do you mean it's "not an exact measure of elapsed time". Do we need to know time down to planck lengths? what *is* the planck length of time? – Timothy Groote Sep 06 '13 at 08:20
  • On a note of actually interesting questions, @MattJohnson : what *is* your personal vendetta with the .NET framework's date/time related portion? – Timothy Groote Sep 06 '13 at 08:22
  • A year is not an exact measure of time because you have to know *which* years you are talking about. In the Gregorian calendar system that most of us use, some years have 365 days and some have 366. If you average it out, one might say that there are 365.242 days in a year, but that's not how it works in real life. We don't add a quarter of a day each year, we add one whole day every leap year. – Matt Johnson-Pint Sep 06 '13 at 12:39
  • With regards to Planck time, that is a real thing and you can read about it [here](http://en.wikipedia.org/wiki/Planck_time) but has nothing to do with this discussion. (I think you were being sarcastic, but it's hard to tell in comments.) – Matt Johnson-Pint Sep 06 '13 at 12:40
  • And I wouldn't call it a *vendetta*, I just find date and time issues fascinating. It's more of a hobby. :) .NET in particular has some design characteristics that create real-world problems for developers all the time and they often don't even know it until a production bug is encountered. I personally have spent countless hours debugging other people's software to find that the root of the problem was a misunderstanding of the API. It is my hope to educate others on these pitfalls, which is part of what StackOverflow is all about. – Matt Johnson-Pint Sep 06 '13 at 12:46
  • You can read more [on my blog](http://codeofmatt.com/2013/04/25/the-case-against-datetime-now/), but I'm not the only one who feels this way. You can also read [Jon Skeet's position](http://noda-time.blogspot.com/2011/08/what-wrong-with-datetime-anyway.html) on this. Ultimately, the best solution for .NET is [Noda Time](http://nodatime.org/), but not everyone is going to run out and adopt it, so I still respond to regular date/time questions. Do you have a problem with that? – Matt Johnson-Pint Sep 06 '13 at 12:47
  • Also, in the post you linked. Read Jeffrey Hantin's answer. – Matt Johnson-Pint Sep 06 '13 at 12:51
  • Dang, that was a little shortsighted of me. Thanks for the follow up explanation, Matt! – Timothy Groote Sep 06 '13 at 13:38
  • No problem. If you learned something, then my work was a success. ;) – Matt Johnson-Pint Sep 06 '13 at 14:12