title: Function to convert string (yyyyMMddhhmm) into DateTime in C#
body: <p>In my database I have a field date of type <strong>varchar</strong> where the date is stored in the following format <strong>yyyyMMddhhmm</strong>, with no spaces or other characters separating them.</p>
  
  <p>Now I need to compare this date with a C# <strong>DateTime</strong>, therefore I need to convert the string into <strong>DateTime</strong>. The most logical way that I can think of is to extract from the variable <code>date</code> the sub-string related to year, month and day and create a new <strong>DateTime</strong> object:</p>
  
  <pre><code>var year = Convert.ToInt32(date.Substring(0, 4));
  
  var month = Convert.ToInt32(date.Substring(4, 2));
  
  var day = Convert.ToInt32(date.Substring(6, 2));
  
  DateTime dateToCompare = new DateTime(year, month, day);
  </code></pre>
  
  <p>Is there any available C# method that allows me to perform this conversion without writing all this code? </p>