Datetime conversion in C# - using formats
<p>I need to convert a <code>String</code> to <code>DateTime</code> format, for this I just tried like</p>

<pre><code>DateTime.ParseExact(DateOfBirth,"MM/dd/yyyy",CultureInfo.InvariantCulture);
</code></pre>

<p>it's working fine when I pass the value like <code>05/30/2012</code>. </p>

<p>But if I try to pass the value as <code>5/30/2012</code> its showing error:</p>

<blockquote>
  <p>String was not recognized as a valid DateTime</p>
</blockquote>

<p>To fix this I tried like</p>

<pre><code>DateTime.ParseExact(String.Format("{0:MM/dd/yyyy}", DateOfBirth), "MM/dd/yyyy",
                                                  CultureInfo.InvariantCulture);
</code></pre>

<p>it's still not working. Here If I try <code>String.Format("{0:MM/dd/yyyy}", DateOfBirth)</code> for the value <code>5/30/2012</code> its showing the same format instead of <code>05/30/2012</code>.</p>

<p>How can I fix this, can anyone help me here...</p>

<p>check this link
<a href="http://stackoverflow.com/questions/336226/string-to-datetime-conversion-in-c-sharp">string to DateTime conversion in C#</a></p>
