首页 > 解决方案 > “en-GB”和“en-Us”文化名称之间的区别

问题描述

当我将此代码日期设置为“12/12/2018 20:08”之类的东西时,没关系

{
     var newCulture = new CultureInfo("en-GB");
     var date = (value as DateTime?)?.ToString(newCulture.DateTimeFormat.ShortDatePattern+" HH:mm", CultureInfo.InvariantCulture);
}

但是当我将文化名称更改为“en-Us”时,结果是“12.12.2018 20:08”(我知道的格式不正确)-返回 ShortDatePattern 的日期格式不正确

我在另一个桌面上对其进行了测试,它可以与其中的 2 个一起正常工作。可能对桌面设置有某种依赖?为什么日期格式多种多样?

标签: c#.netdatetimedatetime-format

解决方案


new CultureInfo("en-GB") calls the ctor with useUserOverride set to true. This means that if you server environment has a match for the specified culture with custom settings those will be used instead of the default once. So to fix your issue CultureInfo("en-GB", false); or CultureInfo.GetCultureInfo("en-GB") (for a cached version)

The GetCultureInfo method retrieves a cached, read-only CultureInfo object. It offers better performance than a corresponding call to the CultureInfo.CultureInfo(String) constructor.

If name is the name of the current culture, the returned CultureInfo object does not reflect any user overrides. This makes the method suitable for server applications or tools that do not have a real user account on the system and that need to load multiple cultures efficiently.


推荐阅读