title: SQL server DateTime and C# DateTime
body: <p>On my SQL server I have a very simple table for testing, which only contains three rows:
  <code>ID</code>, <code>Date</code> and <code>Hours</code>.(<code>varchar</code>, <code>DateTime</code>, <code>varchar</code>).</p>
  
  <p>The DateTime format in SQL is like: <code>yyyy-MM-dd HH:mm:ss.fff</code>.</p>
  
  <p>The DateTime format in C# is like: <code>yyyy-dd-MM HH:mm:ss.fff</code>.</p>
  
  <p>I use the following code to get C# DateTime format:</p>
  
  <pre><code> string ddd = "2012-10-10 00:00:00.000";
   dt1 = DateTime.ParseExact(ddd, "yyyy-MM-dd HH:mm:ss.fff", null);
  </code></pre>
  
  <p>If I try to make it into <code>yyyy-dd-MM</code> I get an error: </p>
  
  <blockquote>
    <p>The DateTime, which is represented by the string, isn't supported by
    the calender.</p>
  </blockquote>
  
  <p>In my C# application is am trying to find total amount of hours between sunder dates. I sort of got it to working, but it only works for dates between 01 and 12. </p>
  
  <p>So like from <code>2012-01-10</code> to <code>2012-10-10</code> (ten days) will give me the correct amount of total hours from the database.</p>
  
  <p>But when I write <code>2012-01-10</code> to <code>2012-14-10</code> (fourteen days) I get an error:</p>
  
  <blockquote>
    <p>The conversion of a char data type to a datetime data type resulted
    in an out-of-range"</p>
  </blockquote>
  
  <p>Thank you in advance.</p>
  
  <p>PS. Can you suggest a easier way to get dates?</p>
  
  <p>mySQL query</p>
  
  <pre><code>string CommandText = "SELECT * FROM date_test WHERE id = '4' AND date BETWEEN '" + dt1 + "' AND '" + dt2 + "'";
  </code></pre>
  
  <p>I have figured out the problem but not the solution.</p>
  
  <p>The problem is that SQL database looks at the format, and wants yyyy-MM-dd, but C# can only send yyyy-dd-MM.</p>
  
  <p>Why cannot <code>ParseExact()</code> do yyyy-MM-dd?</p>