0

Is there a method in C# which can compare dates like the SQL DateDiff method, including time part argument?

For example I have two dates:

DateTime early = "2013.02.01 07:31:15";
DateTime soon = "2013.02.01 23:48:35";

if (Compare(early, soon, DAY) == true)
{
   //I want to be here
}

I want to get info that these two objects are equal in year, month and day; the rest is not important to me.

J. Steen
  • 15,470
  • 15
  • 56
  • 63
Jacek
  • 11,661
  • 23
  • 69
  • 123
  • you could always add `using Microsoft.VisualBasic` and use the VB.NET assembly to do the Compare.. – MethodMan Feb 01 '13 at 15:31
  • Somewhat related: http://stackoverflow.com/questions/683037/how-to-compare-only-date-without-time-in-datetime-types-in-c – Cᴏʀʏ Feb 01 '13 at 15:50
  • You ever heard of the `Kiss Method` `Keep It Simple Stupid` here is a simple solution `if (String.Format("{0:ddMMyyyyHHmmss}", early) == String.Format("{0:ddMMyyyyHHmmss}", soon)) { // success }` – MethodMan Feb 01 '13 at 15:51
  • @DJKRAZE: That solution introduces issues with culture, performance, and is *not* as simple as some of the other solutions provided. – Cᴏʀʏ Feb 01 '13 at 15:55

7 Answers7

5

You can use DateTime.Date:

if(early.Date == soon.Date)
{
} 

if you want accurancy of hours:

if(early.Date == soon.Date && early.Hour == soon.Hour)
{
} 

But, with this solution you will have "12/03/1857 23:59" != "13/03/1857 00:00". If you want to check their "distance":

if(early.Substract(soon).Duration() <= TimeSpan.FromHours(1))
{
}

.Duration() gets the "absolute" value of the time span (because early.Substract(soon) can be "positive" or "negative").

Cédric Bignon
  • 12,892
  • 3
  • 39
  • 51
  • Yes, you have right :) But if I want to compare DateTime with accuracy to hours? – Jacek Feb 01 '13 at 15:32
  • @Bruno.cf: `Date` and `Day` are different. – Cᴏʀʏ Feb 01 '13 at 15:34
  • Bruno.cf you need to look at the `Date` if the months are different then the dates do not equal..so your comment makes `0` sense – MethodMan Feb 01 '13 at 15:36
  • 1
    @AshBurlaczenko `I want to get info that these two objects are equal in year, month and day; the rest is not important to me.`. What part of that statement led you to the conclusion this solution is wrong? – Daniel Kelley Feb 01 '13 at 15:38
4

You could use the Date property of your DateTimes to just get the date portion.

In your case you could use:

if (early.Date == soon.Date)
{
    ....
}
Daniel Kelley
  • 7,579
  • 6
  • 42
  • 50
1

You can use DateTime.Subtract method to find the difference.

System.TimeSpan diff1 = date2.Subtract(date1);

Edit

If you want to compare for equality you can simply use equality operator ==

if(date1 == date2)
{
}
Adil
  • 146,340
  • 25
  • 209
  • 204
  • The addition and subtraction operators are overloaded for `DateTime` to return a `TimeSpan`, so `date2 - date1` would be an equivalent alternative. – Cᴏʀʏ Feb 01 '13 at 15:35
1

I don't think there's anything wrong with being a little verbose if you want to be explicit about how you're comparing two dates:

public static bool YearMonthDayHourDateCompare(DateTime one, DateTime two)
{
    return (one.Year  == two.Year  &&
            one.Month == two.Month &&
            one.Day   == two.Day   &&
            one.Hour  == two.Hour  && 
            ...);
}

Name the method in a way that describes your comparison. Better yet, this could be an extension method:

public static bool CompareDateAndHour(this DateTime one, DateTime two)
{
    return (one.Date == two.Date &&
            one.Hour == two.Hour && 
            ...);
}

Which could be used like:

bool datesAreEqual = soon.CompareDateAndHour(two);
Cᴏʀʏ
  • 105,112
  • 20
  • 162
  • 194
  • 1
    +1 for being explicit being a good thing. But forgive me for this... "*nothing wrong with being explicit*" and then you hide it in `MyCustomDateComparer`? ;) – J. Steen Feb 01 '13 at 15:39
  • @J.Steen: Ok, the name of the function stinks -- it was just an example. Perhaps the sample code would be better not wrapped in a function at all. Would you agree? – Cᴏʀʏ Feb 01 '13 at 15:40
  • I wasn't *too* serious, here, though I'm still always advocating methods and variables having descriptive names. And in this rather lengthy case, a descriptive method name would be better - in my opinion - than having the condition not wrapped at all. =) – J. Steen Feb 01 '13 at 15:42
  • Ouch. Yeah, to the extreme. =) No offence meant, at all. – J. Steen Feb 01 '13 at 15:44
  • @J.Steen: Good point. I should be more careful with sample code on [so]. – Cᴏʀʏ Feb 01 '13 at 15:44
1

If you want to know if is the same day, you cant use date diff...

Try this code:

if(new TimeSpan(early.Ticks).TotalDays() == new TimeSpan(soon.Ticks).TotalDays()){
     // Equal
}
Bruno Croys Felthes
  • 1,183
  • 8
  • 27
0
var diff=soon-early;
diff.TotalHours or diff.TotalMinutes etc...
ebattulga
  • 10,774
  • 20
  • 78
  • 116
0

To compare two dates to an accuracy of hours, use something like this:

bool match = (soon - early).TotalHours < *your diff limit*;
James South
  • 10,147
  • 4
  • 59
  • 115