Where is the locale / culture of the system for .Net

I have a problem with a C # build (.net 2.0 written using Visual studio 2005) installed on a UK server and using the UK regional settings.

What my code does is convert the date in the form of dd / MM / yyyy to utc. those. yyyy-mm-dd. The problem arose with dates such as 02/16/2010, when the component was unable to convert the date and returned an error. After debugging, I realized that, for some strange reason, CultureInfo returned by System.CultureInfo is en-US.

I can programmatically change these settings using:

Thread.CurrentThread.CurrentCulture = new CultureInfo("en-GB", false); 

and my code is working fine.

However, I do not want to do this all the time, since my system should be UK. Not to us. So, how do I change the default culture for the .Net framework by default en-GB instead of en-US?

For information:

  • I tried to update the machine.config file and specify culture = en-GB for the globalization partition (it was set to neutral), but it also did not work [did it for 1.1 and 2.0], but it is possible I did not change it correctly.
  • I checked my regional settings for Windows and is definitely configured for the UK with dates like dd / MM / yyyy
  • I work on a virtual server and checked my host system. It is also installed in the UK.

Edit:

A bit of additional information about the context. The corresponding assembly is called through COM interaction from its own C ++ component, which works as a COM + application.

+6
c # locale
source share
7 answers

You do not need to change CurrentCulture to convert. If you are sure that the date is in the form "dd / MM / yyyy", you can use

 DateTime dtTemp = DateTime.ParseExact(dateString, "dd/MM/yyyy", null) // in order not to have to specify a FormatProvider 

and then use

 dtTemp.ToString("yyyy-MM-dd") 

This way you will have no problems no matter what CurrentCulture is. However, if you are not sure that the date has the form "dd / MM / yyyy", rather, it is based on the short date format CurrentCulture, then you should use

 DateTime dtTemp = DateTime(dateString, CurrentCulture.DateTimeFormat.ShortDatePattern, CurrentCulture.DateTimeFormat); 
+1
source share

The server is configured incorrectly. Control Panel + Area and Language, Location Tab. Changing this can be a bit complicated. The server may have been configured incorrectly. Talk to your server administrator first before doing anything.

Your backup plan is to use the DateTime.TryParse () method overload, which takes an IFormatProvider argument. Pass CultureInfo.GetCultureInfo ("en-gb"). DateTimeFormat.

+5
source share

Hmmm, according to the API Docs :

When a thread starts, its culture is initially defined using GetUserDefaultLCID from the Windows API.

This method infers its locale from (as the name implies) User Default Locale, which I assume is in the control panel. NOTE. This is NOT the same as the user interface locale.

+3
source share

Thanks for your answers (andy sent a question on my behalf). This is really a problem with the regional settings, but neither with the user with whom I was connected, nor with the user in whom this process works. That would be too easy. It looks like the default user was still in the USA. I did a reset by checking the "Apply settings to current user and default user ..." checkbox on the "Advanced" tab and rebooting the server. Now System.Globalization.CultureInfo returns {en-GB}. And MyDate.ToString (yyyy-mm-dd) works fine whether the date is passed as dd / MM / yyyy or dd-MM-yyyy or yyyy-MM-dd without the need for parsing.

However, thank you all for your suggestions (ParseExact, etc.) that really worked. They are not well suited for other date formats that I could not handle in a beautiful way (yyyyMMdd).

Mark

+3
source share

I believe this is represented by System.Globalization.CultureInfo.InstalledUICulture, so if nothing else, maybe you cannot copy it to the current thread culture. I am surprised that you have found a case where the thread culture is different from the established culture. Perhaps your code is running in a process that has changed culture?

Perhaps the account that runs the code has different regional settings than the system one. Have you checked this?

+2
source share

To customize the culture and culture of the user interface for all pages, add the globalization section to the Web.config file, and then set the attributes for the crop and crop, as shown in the following example:

<globalization uiCulture="en" culture="en-GB" />

+1
source share

Assemblies in the .net infrastructure are culture neutral.

What code are you trying to convert date to? If you are using Parse or TryParse , try providing a culture argument to figure out the date.

0
source share

All Articles