"en-GB" 和 "en-Us" 文化名称之间的区别
Difference between "en-GB" and "en-Us" cultures names
当我编写此代码时,日期设置为“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 的日期格式不正确
我在另一个桌面上对其进行了测试,它可以在其中两个桌面上正常工作。
可能对桌面设置有某种依赖性?
为什么日期格式多种多样?
new CultureInfo("en-GB")
调用 ctor 并将 useUserOverride
设置为 true。这意味着如果您的服务器环境与具有自定义设置的指定文化相匹配,将使用这些设置而不是默认设置一次。因此,要解决您的问题 CultureInfo("en-GB", false);
或 CultureInfo.GetCultureInfo("en-GB")
(对于缓存版本)
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.
当我编写此代码时,日期设置为“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 的日期格式不正确
我在另一个桌面上对其进行了测试,它可以在其中两个桌面上正常工作。 可能对桌面设置有某种依赖性? 为什么日期格式多种多样?
new CultureInfo("en-GB")
调用 ctor 并将 useUserOverride
设置为 true。这意味着如果您的服务器环境与具有自定义设置的指定文化相匹配,将使用这些设置而不是默认设置一次。因此,要解决您的问题 CultureInfo("en-GB", false);
或 CultureInfo.GetCultureInfo("en-GB")
(对于缓存版本)
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.