Reputation: 769
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
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
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