(You're mixing ParseExact
and TryParseExact
.)
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 setting
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("da-DK");
and then doing the above ParseExact
? It will fail because the DateSeparator
is not "/"
in this culture. To fix it, either say
DateTime.ParseExact("10/10/2012", @"dd\/MM\/yyyy", null)
or say
DateTime.ParseExact("10/10/2012", "dd/MM/yyyy", CultureInfo.InvariantCulture)
The first works by "escaping" the slash /
so it becomes a literal slash and is not translated to the current DateSeparator
. The second one works by giving the invariant culture where we know that the DateSeparator
is indeed "/"
.
Addition:
If you never set CurrentCulture
in 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.