I’m trying to convert a custom string into a DateTime. I’ve seen several blogs and forum threads use this technique to manage a custom format.
string mysqlDateFormat = "yyyy-M-dd hh:mm:ss"; // MySQL Date Format expressed in C# custom date format specifiers (e.g. 2011-6-07 06:18:14)
DateTime inputTestCompletedDateTime = new DateTime();
if (!DateTime.TryParse(inputTestCompletedDateTimeAsString, out inputTestCompletedDateTime))
{
inputTestCompletedDateTime = DateTime.ParseExact(inputTestCompletedDateTimeAsString, mysqlDateFormat, System.Globalization.CultureInfo.InvariantCulture);
}
However, this code throws an error on the line within the if statement, " Exception Details: System.FormatException: The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.".
I’ve attempted modifications but they’ve all been less successful (where the string is not recognized as valid). However, I thought using the “CultureInfo.InvariantCulture” was the secret to processing custom (i.e. non-Gregorian) formatting.