Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does DateTime.ParseExact throw an exception with "dd.Mm.yyy" format and a date with a 4-digit year? [duplicate]

The documentation on yyy format says that it's 3 digits or more. So why does the following code throw an exception?

string format = "dd.MM.yyy";
string s = DateTime.Now.ToString(format, CultureInfo.InvariantCulture);
DateTime d = DateTime.ParseExact(s, format, CultureInfo.InvariantCulture); //this throws FormatException

Fiddle

like image 978
SlimShaggy Avatar asked Nov 26 '25 23:11

SlimShaggy


1 Answers

This looks like a bug to me. I tried this to rule out any locale problems and also got the same error:

 string format = "dd.MM.yyy";
 string s = DateTime.Now.ToString(format, CultureInfo.InvariantCulture);
 DateTime d = DateTime.ParseExact(s, format, CultureInfo.InvariantCulture); //this throws FormatException

Edit: Looks to be a duplicate of this question: Why can't DateTime.ParseExact parse DateTime output?

like image 141
Jesper Avatar answered Nov 29 '25 11:11

Jesper



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!