title: How to get previous day using datetime
body: <p>I want to set a <code>DateTime</code> property to the previous day at time 00:00:00. I've tried using <code>DateTime.AddDays(-1)</code> and <code>DateTime.AddTicks(-1)</code> and they aren't working. Is this the right way to do it?</p>
  <p>I have 2 objects. Each object has two <code>DateTime</code> fields: <code>ValidFrom</code> and <code>ValidTo</code>.</p>
  <p>EDIT: After coming home from work I tried to get the same behavior as my business objects. The code I used to try and replicate how it functions at work is below. Of course this is working at home but not at work. The good thing is I got good answers and +1 on all! =)</p>
  <pre><code>public class RuleValue
  {
      public DateTime ValidFrom, ValidTo;
      
      public RuleValue(DateTime _validFrom, DateTime _validTo)
      {
          ValidFrom = _validFrom;
          ValidTo = _validTo;
      }
  
      // oldObject.ValidFrom = 1900-01-01
      // oldObject.ValidTo = 9999-12-31
  
      // newObject.ValidFrom = 2010-03-22
      // newObject.ValidTo = 9999-12-31
      public void ChangeOldDate(RuleValue oldObject, RuleValue newObject)
      {
          /* 
           * 1: When first object (oldObject) have ValidTo set to SQL-server maxdate (9999-12-12 23:59:59 etc)
           *    I want to allow for a new object to be created
           * 2: oldObject timespan ValidFrom-ValidTo should not be overlapping with newObjects timespan(i have checks for that)
           * 3: oldObject.ValidTo should be newObject.ValidFrom - one day/or one tick
           */
  
          if (oldObject.ValidTo == DateTime.MaxValue)
          {
              oldObject.ValidTo = newObject.ValidFrom.AddTicks(-1); //now works
          } 
      }
  }
  
  class Program
  {
      static void Main(string[] args)
      {
          RuleValue rv1 = new RuleValue(DateTime.Parse(&quot;1900-01-01&quot;), DateTime.MaxValue);
          RuleValue rv2 = new RuleValue(DateTime.Parse(&quot;2010-03-22&quot;), DateTime.MaxValue);
  
          Console.WriteLine(&quot;First: &quot;);
          Console.WriteLine(&quot;Old = &quot; + rv1.ValidFrom + &quot; - &quot; + rv1.ValidTo);
          Console.WriteLine(&quot;New = &quot; + rv2.ValidFrom + &quot; - &quot; + rv2.ValidTo);
          
          rv1.ChangeOldDate(rv1, rv2);
  
          Console.WriteLine(&quot;After: &quot;);
          Console.WriteLine(&quot;Old = &quot; + rv1.ValidFrom + &quot; - &quot; + rv1.ValidTo);
          Console.WriteLine(&quot;New = &quot; + rv2.ValidFrom + &quot; - &quot; + rv2.ValidTo);
  
          Console.ReadKey();
      }
  }
  //Output:
  //First:
  //Old = 1900-01-01 00:00:00 - 9999-12-31 23:59:59
  //New = 2010-03-22 00:00:00 - 9999-12-31 23:59:59
  //After:
  //Old = 1900-01-01 00:00:00 - 2010-03-21 23:59:59
  //New = 2010-03-22 00:00:00 - 9999-12-31 23:59:59
  //  ALL CORRECT! :D
  </code></pre>