Why does the following code generate a FormatException?
DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", null);

Perhaps it has something to do with the fact that the code is running under IIS 7.5 Express as a part of an MVC3 site execution logic?
You need to include CultureInfo, for example:
DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", new CultureInfo("en-US"));
Slashes in format string are culture sensitive and if you don't pass in CultureInfo, current culture is used. You can also use CultureInfo.InvariantCulture and it will work. Jon Skeet provides some detailed explanation here.
depends on your culture, to take that out of the equation....
DateTime.ParseExact("03/01/2012", "dd/MM/yyyy", CultureInfo.InvariantCulture);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With