title: How do you compare DateTime objects using a specified tolerance in C#?
body: <p>By default C# compares DateTime objects to the 100ns tick. However, my database returns DateTime values to the nearest millisecond. What's the best way to compare two DateTime objects in C# using a specified tolerance?</p>
  
  <p>Edit: I'm dealing with a truncation issue, not a rounding issue. As Joe points out below, a rounding issue would introduce new questions.</p>
  
  <p>The solution that works for me is a combination of those below.</p>
  
  <pre><code>(dateTime1 - dateTime2).Duration() &lt; TimeSpan.FromMilliseconds(1)
  </code></pre>
  
  <p>This returns true if the difference is less than one millisecond. The call to Duration() is important in order to get the absolute value of the difference between the two dates.</p>