DateTime.Parse using culture en-us, despite web.config setting
-
25-05-2021 - |
문제
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.
해결책 2
The problem seems to be that ASP.NET overwrites the culture even when you explicitly specify it. (Like
DateTime.Parse("Whatever", New System.Globalization.CultureInfo("de-ch"))
)
one needs to force override it
New System.Globalization.CultureInfo("de-ch", False)
So in order to make it configurable and change it as few as possible, you need to get the culture from web.config with
System.Globalization.CultureInfo.CurrentCulture.Name
and then force set it with
DateTime.Parse("Whatever", New System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, False))
Note the overload with false, it's necessary, otherwise it doesn't really work.
Here is my solution:
Namespace ASP.NET.Sucks
Public Class PageWithCorrectPageCulture
Inherits Web.UI.Page
Protected Sub New()
System.Threading.Thread.CurrentThread.CurrentCulture = New System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, False)
System.Threading.Thread.CurrentThread.CurrentUICulture = New System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, False)
End Sub
End Class
End Namespace
Then, in the codebehind, replace System.Web.UI.Page with PageWithCorrectPageCulture
Partial Class whateverpage
Inherits PageWithCorrectPageCulture
'Inherits System.Web.UI.Page
And for those who can only copy-pase C#:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
namespace ASP.NET.Sucks
{
public class PageWithCorrectPageCulture : Web.UI.Page
{
protected PageWithCorrectPageCulture()
{
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, false);
System.Threading.Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(System.Globalization.CultureInfo.CurrentCulture.Name, false);
}
}
}
다른 팁
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)