Today I had an awful lot of trouble trying to parse a european datetime string (“16-09-2005”) into a DateTime instance. I used the following code and got a FormatException when running this on the server:
DateTime date = DateTime.Parse("16-09-2005");
Usually, the DateTime.Parse method does a pretty good job but now and then it needs a little guidance. I figured this was one of those moments so I called an overload with a specific IFormatProvider:
DateTime date = DateTime.Parse("16-09-2005", new CultureInfo("nl-NL"));
But this also failed in exactly the same way. Pretty puzzled I called in my collegue and after trying all the different overloads of Parse with al sorts of different CultureInfo instances we still couldn’t get this simple string to be parsed.
Then, my collegue suggested trying to set the culture in the web.config file. First I tried setting this in the web.config of the subfolder containing the .aspx pages but this didn’t work so I put the setting in the web.config file one level higher in the root folder of our application and miraculously this worked. Why the stupid Parse function ignored the explicitly passed CultureInfo instance in the first place remains a mystery.