user2323308
user2323308

Reputation: 769

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.

Upvotes: 5

Views: 10309

Answers (2)

Habib
Habib

Reputation: 223207

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);

Upvotes: 3

nvoigt
nvoigt

Reputation: 77285

If you didn't mean to import the 10th day of the 20th month of the year 611, either your format string or your data is wrong. Did you mean to import with "yyyymmddHHmmss" ?

Upvotes: 1

Related Questions