I am parsing a string to convert it into a DateTime. I am getting in some cases an error:
String was not recognized as a valid DateTime.
I am getting this error only if I run the application from a computer located in a different country than US. What I see in the string is 09/20/2010 14:11
and in this case I get an exception. If I have a value like: 10/05/2010 12:54
I don't get an exception. I suppose it is the fact that the day is 20 and this computer is in Europe, so it thinks that 20 is the month. The problem is I force it to be en-US:
CompletedDttm = DateTime.ParseExact(value, "MM/dd/yyyy hh:mm", new CultureInfo("en-US"));
Because I am getting the exception I suppose sure this is the correct approach.
Any idea how to parse a string in a way that works no matter what culture I am running on the machine?
You should use "HH:mm" instead of "hh:mm" - "HH" is for 24-hour clock; "hh" is for a 12-hour clock. So 14 isn't a valid value for "hh".
I'd expect to see the same problem even on a US machine though... maybe you happened to only get pre-1pm times on your US machines due to time zone differences?
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