Error DateTime.ParseExact

The following lines throw a FormatException.

DateTime dateResult;
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
string dateFormat =  "yyyy-MM-dd HH:mm";
string dateToCheck = "2013-20-10 00:00";

dateResult = DateTime.ParseExact(dateToCheck, dateFormat, provider); // fails

It says

The DateTime represented by the string is not supported on the System.Globalization.GregorianCalendar.

I do not see what happened.

+4
source share
5 answers

The month seems to be 20. 20. The 20th month does not exist. As the comments say, you could mix up the day and the month.

The line should be:

string dateToCheck = "2013-10-20 00:00";
+5
source

, , M d , , /. ( ( H)) :

string dateFormat =  "yyyy-d-M HH:mm";

, : 02,2,20

+1

Change your format with

"yyyy-MM-dd HH:mm"

to

"yyyy-dd-MM HH:mm"

because there is no month 20.ht.

string dateFormat = "yyyy-dd-MM HH:mm";
string dateToCheck = "2013-20-10 00:00";
DateTime dateResult = DateTime.ParseExact(dateToCheck, dateFormat, CultureInfo.InvariantCulture);
Console.WriteLine(dateResult);

Here . DEMO

0
source

There is no 20th month on the calendar. Change your code as:

...
string dateToCheck = "2013-10-20 00:00";
...

Or that:

...
string dateFormat =  "yyyy-dd-MM HH:mm";
...
0
source

do it like this, note that I changed dd and MM

DateTime dateResult;
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
string dateFormat = "yyyy-dd-MM HH:mm";
string dateToCheck = "2013-20-10 00:00";
dateResult = DateTime.ParseExact(dateToCheck, dateFormat, provider);
0
source

All Articles