title: c# - Convert string to datetime with DateTime.ParseExact
body: <p>I have to convert a string extract from a file to datetime. The problem is that my string doesn't have a unique format. For example my string can be something like : 19-05-2016 1:24:09:560 or 19-05-2016 21:24:09:56 or 19-05-2016 10:24:09:560 or 19-05-2016 10:24:09 and so on. I didn't encounter all the possibilities yet ( these strings are pulled out of an json response from an API call) </p>
  
  <p>This is how my code  looks like</p>
  
  <pre><code>public static DateTime ConveDateTime(string a)
  {
      DateTime finished;
      try
      {
          finished = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss:fff", CultureInfo.InvariantCulture,
              DateTimeStyles.AssumeUniversal |
              DateTimeStyles.AdjustToUniversal);
      }
      catch (Exception)
      {
          try
          {
              finished = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss:ff", CultureInfo.InvariantCulture,
                  DateTimeStyles.AssumeUniversal |
                  DateTimeStyles.AdjustToUniversal);
          }
          catch (Exception)
          {
              try
              {
                  finished = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss:f", CultureInfo.InvariantCulture,
                      DateTimeStyles.AssumeUniversal |
                      DateTimeStyles.AdjustToUniversal);
              }
              catch (Exception)
              {
                  try
                  {
                      finished = DateTime.ParseExact(a, "dd-MM-yyyy HH:mm:ss", CultureInfo.InvariantCulture,
                          DateTimeStyles.AssumeUniversal |
                          DateTimeStyles.AdjustToUniversal);
                  }
                  catch (Exception)
                  {
                      try
                      {
                          finished = DateTime.ParseExact(a, "dd-MM-yyyy H:mm:ss:fff", CultureInfo.InvariantCulture,
                              DateTimeStyles.AssumeUniversal |
                              DateTimeStyles.AdjustToUniversal);
                      }
                      catch (Exception)
                      {
                          try
                          {
                              finished = DateTime.ParseExact(a, "dd-MM-yyyy H:mm:ss:ff", CultureInfo.InvariantCulture,
                              DateTimeStyles.AssumeUniversal |
                              DateTimeStyles.AdjustToUniversal);
                          }
                          catch (Exception)
                          {
  
                              finished = DateTime.ParseExact(a, "dd-MM-yyyy H:mm:ss:f", CultureInfo.InvariantCulture,
                              DateTimeStyles.AssumeUniversal |
                              DateTimeStyles.AdjustToUniversal);
                          }                            
                      }
                  }
              }
          }
      }
  
      return finished;
  }
  </code></pre>
  
  <p>I want to know if there is any better solution in converting a string than the solution I have.</p>