简体   繁体   中英

Where does CultureInfo data come from?

When I create a culture info object for en-CA ( new CultureInfo("en-CA") ) I'm getting a different ShortDatePattern in my production environment than in my development environment.

I don't have access to the production server then I'm just assuming that the information that fills the object comes from regional settings.

I tried to change my development regional settings to have a different ShortDatePattern for en-CA but I'm still getting "MM/dd/yyyy" instead of "yyyy-MM-dd"

Where does this configuration come from and what could be the difference between my local environment and the production environment that is returning a different ShortDatePattern for the same CultureInfo?

Code:

var someDate = DateTime.now;
var cultureInfo = new CultureInfo("en-CA");
var formatedDate = someDate.ToString(cultureInfo.DateTimeFormat.ShortDatePattern);

It's the machine culture, in order to force a culture on your app you have to add it in the web config or the app config

To set the UI culture and culture for all pages, add a globalization section to the Web.config file, and then set the uiculture and culture attributes, as shown in the following example:

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

here is the MSDN Source

That's because the CultureInfo constructor uses the customised user configuration by default. Use the constructor that takes an extra bool parameter useUserOverride and pass false .

From the documentation

useUserOverride

A Boolean that denotes whether to use the user-selected culture settings (true) or the default culture settings (false).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM