title: C# : How to convert string to DateTime, where the string can have any of the standard datetime format
body: <p>I had posted a question on DateTime to String conversion, I got many satisfying answers for that .. so I thank StackOverflow very much ..<br> Here is one more problem of String manupulation, I am stuck with ..<br><br> I have to convert a string (from some external source) using C# code .. the string can have these expected format of DateTime ..<br></p>
  
  <ol>
  <li><code>02/31/2009 01:59:59</code> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 24 hours format</li>
  <li><code>02/31/2009 01:59:59 AM</code> &nbsp;&nbsp;&nbsp; 12 hours format</li>
  <li><code>2/31/2009 1:59:59</code></li>
  <li><code>2/31/2009 1:59:59 AM</code></li>
  <li><code>02/01/2009 01:59:59 AM</code></li>
  <li><code>2/1/2009 1:59:59</code> <br></li>
  <li>and so on ....... </li>
  </ol>
  
  <p>I tried using <code>DateTime(Convert.ToInt32(string_date.Substring(6,4)),Int,Int,Int,Int,Int,Int)</code> <br><em>ie, By extracting the values of month, Day etc</em><br><br>But it doesn't work .. because I can't extract the values with substring perfectly .. as the length of string is  <strong>Varying</strong> <br> I also have tried to extract the values referring the occurance of "/", "<i>space</i>" and ":" but it becomes bottle neck to derive with (non-)Occurrence of AM/PM<br><br> <em>Only the length of Day, Month and Hours can vary ..</em> <br></p>