String was not recognized as a valid DateTime (DateTime.Parse("16.10.2014"))

When i Try to convert string to datetime, it thrown an error & it says that String was not recognized as a valid DateTime.

My code is below:

string dateString = "16.10.2014";
DateTime formattedDate = DateTime.Parse(dateString);
Jon Skeet
people
quotationmark

Just use DateTime.ParseExact to specify the format. That's pretty much always the solution when you know the format ahead of time:

DateTime date = DateTime.ParseExact(
    dateString,
    "dd.MM.yyyy", // This might want to be d.M.yyyy - we don't know
    CultureInfo.InvariantCulture);

The difference between dd.MM.yyyy and d.M.yyyy is how single-digit months and days are handled. Would July first be 01.07.2016 or 1.7.2016? Adjust your format string to suit the data.

Note the use of CultureInfo.InvariantCulture - this avoids parsing the date as if it's in a non-Gregorian calendar if your system culture happens to be one which uses a different calendar.

people

See more on this question at Stackoverflow