C# – DateTime.Parse throws exception “not support in System.Globalization.GregorianCalendar”

c++datetimestring

string formatString = "MMddyyyyHHmmss";
string sample = "20100611221912";
DateTime dt = DateTime.ParseExact(sample, formatString, System.Globalization.CultureInfo.InvariantCulture);

the specific exception thrown is:

System.FormatException: The DateTime represented by the string is not supported in calendar System.Globalization.GregorianCalendar.

Best Solution

Your format should be:

string formatString = "yyyyMMddHHmmsss";

(It can also be "yyyyddMMHHmmsss", if it is 06-Noveber-2010)

Considering your Date is dt = {11/06/2010 10:19:12 PM} (11-June-2010)

For your current format:

MMddyyyyHHmmss
20100611221912

MM can't be 20, since MM stands for Month. So your code should be:

string formatString = "yyyyMMddHHmmsss";
string sample = "20100611221912"; 
DateTime dt = DateTime.ParseExact(sample, formatString, System.Globalization.CultureInfo.InvariantCulture);