Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateTime.Parse throws exception "not support in System.Globalization.GregorianCalendar"

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.

like image 342
user2323308 Avatar asked Oct 04 '22 04:10

user2323308


1 Answers

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);
like image 111
Habib Avatar answered Oct 10 '22 03:10

Habib