I have a code that looks like code below in a PC which runs fine:
DateTime.Parse("10/10/2012");
I copied it over to other machine and it says something blaming the date conversion is wrong.
What I had to do to work around it is to use:
DateTime.TryParseExact("10/10/2012", "dd/MM/yyyy", null);
Does anyone know if there is any configuration in c# or the machine that control this such things? (or it might be .net version different between the two computers? but i am pretty sure they are the same in both machine as I used Ms Visual Studio 2008 for both machine).
I am talking about global configuration on windows itself that makes some strict exceptions to my programs. Anyone knows about this?
(You’re mixing
ParseExactandTryParseExact.)The differences come from the
CultureInfo.CurrentCulture.Different parts of the world write dates in different ways.Even saying
ParseExact("10/10/2012", "dd/MM/yyyy", null)can be problematic. Have you tried first settingand then doing the above
ParseExact? It will fail because theDateSeparatoris not"/"in this culture. To fix it, either sayor say
The first works by “escaping” the slash
/so it becomes a literal slash and is not translated to the currentDateSeparator. The second one works by giving the invariant culture where we know that theDateSeparatoris indeed"/".Addition:
If you never set
CurrentCulturein your code, its value depends on the Regional and language settings of your operating system. I suppose you’re running Windows? Details might depend on exact Windows version.