title: How to check date format c#
body: <p>I have a c# block of code here: </p>
  
  <pre><code>    string inputString = textBox1.Text;
      DateTime dt;
      try
      {
          if (DateTime.TryParseExact(inputString, "yyyyMMdd", null, DateTimeStyles.None, out dt) == true)
          {
              dt = dt.AddMonths(6);
              textBox2.Text = dt.ToString("yyyyMMdd");
          }
          else if (DateTime.TryParseExact(inputString, "yyyy.MM.dd", null, DateTimeStyles.None, out dt) == true)
          {
              dt = dt.AddMonths(6);
              textBox2.Text = dt.ToString("yyyyMMdd");
          }
      }   
      catch (Exception ex) { MessageBox.Show(ex.Message); }
  </code></pre>
  
  <p>Basically, what it does is, user input a string in textbox1, on button click, C# will check what date format is in the textbox, then add 6 months on the date and output it in the textbox2 to string format yyyyMMdd. As you see as of now, it accepts  yyyyMMdd and yyyy.MM.dd and do the same process. But my problem is I still got a lot of date format left:</p>
  
  <pre><code>dd-MM-yy 
  yyyy/mm/dd 
  yyyy-mm-dd 
  yy/mm/dd 
  </code></pre>
  
  <p>I don't want to use OR in my IF statement. Is there a way like WHATEVER the format is, will be accepted and do the process.
  Thanks a lot guys!</p>