DateTime.Parse using culture en-us, despite web.config setting

Stefan Steiger picture Stefan Steiger · Mar 20, 2012 · Viewed 7.9k times · Source

Question:

in web.config in section

system.web

I have

<globalization culture="de-ch" uiCulture="de-ch" requestEncoding="UTF-8" responseEncoding="UTF-8"/>

What I want is to parse a string like this

"20.03.2012 00:00:00"

to a datetime value

but

DateTime dtAsIs = DateTime.Parse("20.03.2012 00:00:00")

throws an exception

Unfortunately only on the testserver, not on my development system. I do not have access to the testserver, except to copy the webapp over into a windows share.

I can reproduce the exception like this:

DateTime dtThrowsException = DateTime.Parse("20.03.2012 00:00:00",new System.Globalization.CultureInfo("en-us"));

Whereas it works fine like this:

DateTime dtWorks = DateTime.Parse("20.03.2012 00:00:00",new System.Globalization.CultureInfo("de-ch"));

I checked the ASP page, and there is NO culture set in the asp page

(I mean this:

<% @Page Culture="fr-FR" Language="C#" %>

)

If I set

System.Threading.Thread.CurrentThread.CurrentCulture

and

System.Threading.Thread.CurrentThread.CurrentUICulture

to de-ch at the very start of Page_Load like this

System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("de-ch");
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo("de-ch");

then it works fine.

The browser language is set to "de-ch", I checked that.

Can anybody tell my why the thread-culture gets set to English ?

I mean the obvious reason is that the server operating system is English, but I can't change that one, only settings in web.config.

Answer

Hanno picture Hanno · Mar 20, 2012

I have the same experience as you, it seems that the globalization tag in web.config is simply ignored. But since you always want to parse dates in the de-ch culture, I don't see what's wrong with just providing the culture to the DateTime.Parse method (some guidelines say this is the best thing to do anyway)