I’m trying to put a variable, stored in a string format in a dateTime variable.
System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo("en-US");
System.Globalization.DateTimeFormatInfo usaDateFormatInfo = culture.DateTimeFormat;
string sDataStored = "10/15/2011";
if (DateTime.TryParse(sDataStored , usaDateFormatInfo, System.Globalization.DateTimeStyles.None, out TestedDateTime))
DateTime dMyUSDateTime = TestedDateTime;
Unfortunately, the final result in my variable is not : “10/15/2011” but “15/10/2011” (the french culture, which is the current culture of the application for the moment).
Same result with the TryParseExact.
I could go thru a “Convert”, inside a “try/catch”, but Im sure there are other better way to solve this problem…
Thanks for your help.
When you say the result is
15/11/2011where are you seeing that? In the debugger? The debugger will just format the variable according to your current culture (by just calling ToString).The DateTime object doesn’t stored the culture it was parsed from. You need to pass the culture to it when you convert it back to a string so it formats according to the US culture.
e.g.