title: Difficulty comparing two DateTime instances
body: <p>I'm writing a Unit Test class in C# (.NET 4.5). In one of the tests I'm checking the values of various properties after an instance of our class <code>FeedbackDao</code> is constructed.  On construction, the <code>FeedbackDate</code> property of <code>FeedbackDao</code> is set to <code>DateTime.Now</code>. </p>
  
  <pre><code>FeedbackDao feedbackDao = new FeedbackDao();
  // a couple of lines go here then I set up  this test:
  Assert.IsTrue(feedbackDao.FeedbackDate.CompareTo(DateTime.Now) &lt; 0);
  </code></pre>
  
  <p>My assumption is that feedbackDao.FeedbackDate should always be just a little earlier than the current time returned by <code>DateTime.Now</code>, even if it's only by a millisecond, and my <code>IsTrue</code> test should always pass, but sometimes it passes and sometimes it fails. When I add a message like this:</p>
  
  <pre><code>Assert.IsTrue(feedbackDao.FeedbackDate.CompareTo(DateTime.Now) &lt; 0,
                  feedbackDao.FeedbackDate.CompareTo(DateTime.Now).ToString());
  </code></pre>
  
  <p>the message sometimes reads -1 (meaning that <code>FeedbackDate</code> is earlier than <code>Now</code>) and sometimes reads 0 (meaning that the DateTime instances are equal).</p>
  
  <p>Why is  <code>FeedbackDate</code> not <strong>always</strong> earlier than <code>Now</code>? And, if I can't trust that comparison, how can I write a rigorous test to check the value of <code>FeedbackDate</code> when <code>FeedbackDao</code> is constructed?</p>