Instances of CultureInfo (from same culture) chang

2019-06-21 13:14发布

问题:

I've a web site which writes a date like this:

CultureInfo cultureInfo = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(c => string.Equals(c.TwoLetterISOLanguageName, MvcApplication.Language));
return string.Concat(date.Day, ".", cultureInfo.DateTimeFormat.GetAbbreviatedMonthName(date.Month));

In both my PC (Windows 7, Service Pack 1, Spanish culture) and the server (Windows Server 2012, English Culture) the MvcApplication.Language is es so the culture I get from the list is: es-ES.

I'd expect they both write the same string (they have different cultures, but I'm not using CurrentCulture). However the results are abr in my PC and Abr. in the server.

I've tried this isolated from MvcApplication.Language using the LinqPAD but it is still different. I've printed the arrays of AbbreviatedDayNames and AbbreviatedMonthNames and they are different in each computer.

Am I doing something wrong or this is the expected behavior? How can I do to make it completely independent from Windows / User Culture?

回答1:

A you can see in this link from MSDN, culture info can be different on some windows versions.

Remember that the culture names and identifiers represent only a subset of cultures that can be found on a particular computer. Windows versions or service packs can change the available cultures. Applications add custom cultures using the CultureAndRegionInfoBuilder class. Users add their own custom cultures using the Microsoft Locale Builder tool. Microsoft Locale Builder is written in managed code using the CultureAndRegionInfoBuilder class.

If you want some format to be static, you can build your own CultureInfo object and customize the behaviour of it, setting your own Date Time format, as an example