.net - 为什么 DateTime.ToShortTimeString() 不尊重 "Regio

我遇到了一个问题,这可能是由于我对 DateTime.ToShortTimeString() 方法的工作原理有误解。使用此函数格式化时间字符串时,我假设它会尊重 Windows 7 格式设置中的“短时间”设置

Control Panel -> Clock, Language and Region -> Region and Language -> Formats Tab.

However .NET seems to select a short time format not based upon this setting but based upon the current culture:

Region and Language -> Location -> Current Location

I did some testing on Windows 7 RC:

Culture: en-GB, 6AM: 06:00, 6PM: 18:00 // HH:mm (United Kingdom)
Culture: en-GB, 6AM: 06:00, 6PM: 18:00 // hh:mm (United Kingdom)
Culture: en-US, 6AM: 6:00 AM, 6PM: 6:00 PM // HH:mm (United States)
Culture: en-US, 6AM: 6:00 AM, 6PM: 6:00 PM // hh:mm (United States)
Culture: el-GR, 6AM: 6:00 πμ, 6PM: 6:00 μμ // HH:mm (Greece)
Culture: el-GR, 6AM: 6:00 πμ, 6PM: 6:00 μμ // hh:mm (Greece)

I used el-GR as that was the culture that the user that reported the problem with, he also tested this on Vista SP2 and Win 7 RC with the same result.

The question is two-fold really: 1) What is my misunderstanding of .NET and Windows Formats? 2) What is the best solution to create a short format time string (HH:mm or hh:mm tt) based upon the operating system, ideally this should work in Mono so I would prefer to avoid reading from the registry or P/Invoke.

Method used to generate the above, for future reference and testing.

[STAThread]
static void Main(string[] args)
{
    CultureInfo culture = CultureInfo.CurrentCulture;

    DateTime sixAm = new DateTime(2009, 07, 05, 6, 0, 0); // 6AM 
    DateTime sixPm = new DateTime(2009, 07, 05, 18, 0, 0); // 6PM

    string sixAmString = sixAm.ToShortTimeString();
    string sixPmString = sixPm.ToShortTimeString();

    string format = "Culture: {0}, 6AM: {1}, 6PM: {2}";

    string output = String.Format(format, culture, sixAmString, sixPmString);
    Console.WriteLine(output);
    Clipboard.Clear();
    Clipboard.SetText(output);

    Console.ReadKey();
}

更新: 根据 Mike 在下面的评论,我对上述方法进行了以下更改:

以下两行

string sixAmString = sixAm.ToShortTimeString();
string sixPmString = sixPm.ToShortTimeString();

改为

string sixAmString = sixAm.ToString("t", culture);
string sixPmString = sixPm.ToString("t", culture);

我还更改了文化变量以使用 CultureInfo.CurrentUICulture。

不幸的是,这并没有像我希望的那样工作,无论 Windows 7 格式选项卡中的短时间配置如何,输出都是:

Culture: en-US, 6AM: 6:00 AM, 6PM: 6:00 PM

CultureInfo.CurrentUICulture 似乎总是 en-US。

最佳答案

第二个问题的答案是

DateTimeFormat.Format(DateTime.Now, "t", CultureInfo.CurrentUICulture);

DateTime.Now.ToString("t", CultureInfo.CurrentUICulture);

实际上,使用接受 CultureInfo 的显式方法总是更好。 .Net 如何选择默认使用 CurrentCulture 或 CurrentUICulture 或 InvarinatCulture 的方式并不一致。

使答案完整。我还将概述文化之间的差异。

所以 CurrentCulture 是“控制面板 -> 时钟、语言和区域 -> 区域和语言 -> 格式选项卡”。这是您期望您的计算成为的文化。例如,您可以在美国进行会计,因此您必须在美国进行配置。

CurrentUICulture 是“区域和语言 -> 显示语言”,表示当您从乌克兰移民时,您希望您的应用程序本地化为 UA(但所有计算仍在美国)。

InvariantCulture 就是所谓的与文化无关的语言环境。您应该使用它来存储信息等。实际上它是 En-US。

注意:每个设置位于 windows 中的位置可能是错误的。但你可能有一个想法。

关于.net - 为什么 DateTime.ToShortTimeString() 不尊重 "Regional and Language Settings"中的短时间格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1292246/

相关文章:

python - Gtk3 TextBuffer.serialize() 返回带有格式标签的文本,即

)"修改代码样式">java - groovy intelliJ "angle brackets (<>)"修改代码样式

asp.net-mvc - Resharper 5 : How do I set the defau

之后强制java xml do">java - 如何在<?xml版本="1.0"编码="UTF-8"?>之后强制java xml do

javascript - Visual Studio 2010 坚持在 JavaScript 中插入

html - 不熟悉的 Javascript 语法/hack

c - 如何将 "unbreak"C 代码与 "Artistic Style"

javascript - Bootstrap datepicker 显示的日期格式与值的格式不同

c++ - Eclipse CDT 中的格式(换行)构造函数初始化程序列表

java - java.util.Formattable 上的示例是否不正确?