Convert String to Date in .NET if my incoming date format is in YYYYMMDD

Solution 1:

Use DateTime.ParseExact(). Something like:

   string date = "20100102";
   DateTime datetime = DateTime.ParseExact(date, "yyyyMMdd", CultureInfo.InvariantCulture);

Solution 2:

 DateTime.TryParseExact(myDateString, "yyyyMMdd", 
                         CultureInfo.InvariantCulture, 
                         DateTimeStyles.None, out myDateVar )

Solution 3:

Check DateTime.ParseExact or DateTime.TryParseExact.