c# - “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 个一起正常工作。可能对桌面设置有某种依赖?为什么日期格式多种多样?
解决方案
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.
推荐阅读
- phpmyadmin - 如何将 127.0.0.1.sql 文件导入本地主机
- c# - 使用命名管道将图像从 C++ 发送到 C# 应用程序
- php - 如何将 Soap Env API 响应转换为 PHP 数组或 JSON 数组
- macos - 无法转换“NSStoryboardSegue.Identifier”类型的值?到预期的参数类型“字符串”
- javascript - Firebase 函数 setInterval 和 clearInterval 分别调用
- java - 如何将此jsonarray解析为字符串或数组java
- python - django.core.exceptions.FieldDoesNotExist:BuildingAddress 没有名为“False”的字段
- angularjs - 使用 URL 状态参数中的 ID 传递整个值
- c# - 如何在 Xamarin 表单中处理应用程序初始化任务?
- java - 使用休眠获取数据时的无限循环