.net 如何以正确的格式显示时间?
我有一个 VSTO Outlook 加载项。该插件显示带有 DateTimePicker 控件的 winforms 窗口,它用于选择一天中的时间。我想设置类似于当前 Windows 时间格式的时间格式(如任务栏的时钟)。
为此,我使用以下代码:
this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.CustomFormat = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
在 Windows 区域和语言设置中,我的格式和位置为法语,键盘和语言为英语。
因此,在我的表单中,我看到的是 7:00,而不是 19:00。
CurrentCulture、CurrentUICulture 和 InstalledUICulture 返回“h:mm tt”短时间格式,区域性名称为 en-US。
我的问题是如何确定 Windows 以法语格式 (HH:mm) 显示时间并以这种格式在 DateTimePicker 中显示时间?
- 编辑
当我在单独的控制台应用程序中运行以下代码时,
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern);
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern);
它会返回
高:毫米
时:分:秒
dddd d MMMM yyyy HH:mm:ss
在 Outlook 加载项中,类似的代码返回美国格式:
高:毫米 tt
时:分:秒 tt
dddd、MMMM dd、yyyy 时:分:秒 tt
Outlook 设置为使用默认 Windows 语言(英语)。
I have a VSTO Outlook add-in. This add-in shows winforms window with DateTimePicker contorl, it is used to choose time of day. I want to set time format similar to current Windows time format (like in clock from taskbar).
To do this I use following code:
this.dateTimePicker1.Format = DateTimePickerFormat.Custom;
this.dateTimePicker1.CustomFormat = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
In Windows Region and Language settings I have French in Format and Location and English in Keyboards and Languages.
As a result, in my form I see 7:00 instead of 19:00.
CurrentCulture, CurrentUICulture and InstalledUICulture returns "h:mm tt" shorttime format and culture name is en-US.
My question is how to determine that Windows shows time in French format (HH:mm) and show time in DateTimePicker in this format?
--edit
When I run following code in separate console application
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern);
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.LongTimePattern);
Console.WriteLine(CultureInfo.CurrentCulture.DateTimeFormat.FullDateTimePattern);
it returns
HH:mm
HH:mm:ss
dddd d MMMM yyyy HH:mm:ss
In Outlook add-in similar code returns american format:
h:mm tt
h:mm:ss tt
dddd, MMMM dd, yyyy h:mm:ss tt
Outlook is setup to use default Windows language (English).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不能使用..,而不是使用自定义格式。
这应该以正确的格式显示时间(即用户选择的格式)
--编辑
如果运行以下命令,您会得到什么?
当我的“区域和语言”区域设置为“法语(法国)”时,我得到了这个。
Rather than using custom formats, can't you use..
This should show the time in the correct format (i.e. the one selected by the user)
-- edit
What do you get if you run the following?
With my 'region and language' region set to 'French (France)' I get this.
要获取 Windows 格式的时间,请在单独的线程中显示表单。在 Outlook 线程中显示它时,它需要 Outlook 区域性设置。
To get time in Windows format, show form in separate thread. When showing it in Outlook thread it takes Outlook culture settings.