的CultureInfo&安培; DateTimeInfo:如何检查是否是24小时时间? [英] CultureInfo & DateTimeInfo: How to check if is 24 hour time?

查看:129
本文介绍了的CultureInfo&安培; DateTimeInfo:如何检查是否是24小时时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我修改它使用存储的CultureInfo为每个登录的用户一个全球化的Web应用程序。

I'm modifying a globalized web application which uses stored CultureInfo for each logged in user.

客户想时间数据项进行本地化。作为格式化已经可用显示是没有问题的。不过,我需要检测,如果当前CultureInfo是24小时时间或AM / PM这样我就可以显示正确的输入框(不只是一个文本框)。

The client would like time data entry to be localized. Displaying is not a problem as the formatting is already available. However I need to detect if the current cultureinfo is for 24 hour time or am/pm so I can display the correct input boxes (not just a textfield).

我最初的想法是要检查的CultureInfo的DateTimeInfo财产,看看ShortTimePattern包含一个大写的H或小写h但这个并没有感到强大的足以让我。

My initial idea was to check the DateTimeInfo property of CultureInfo and see if the ShortTimePattern contained a capital H or a lower case h but this didn't feel robust enough for me.

时有没有更好的办法?我读过这两个类的属性,但除非我失去了一些东西,我看不到任何现有的方法或属性。

Is there a better way? I've read the class properties of both but unless I'm missing something, I can't see any existing methods or properties.

推荐答案

我不认为这是一个更好的方式来获取信息。对于文化的时间模式可以包含任何内容(用户甚至可以创建一个自定义的文化,其中ShortTimePattern是\hello,然后则DateTime.ToString()将返回你好任何时间)。在这种情况下,如何可以在框架确定该CultureInfo的是24小时或12小时格式?

I don't think there is a better way to obtain that information. The time pattern for a culture could contain anything (a user could even create a custom culture where the ShortTimePattern is "\hello" and then DateTime.ToString() would return "hello" for any time). In that case how could the framework determine if that CultureInfo is in 24-hour or 12-hour format?

因此​​,一个正常的的DateTimeFormatInfo。 ShortTimePattern 必定包含一个H或H,否则小时将不被显示。我觉得你可以按照你最初的想法,并检查了点。你也可以检查H或H不与\像我的\hello的例子,因为这并不代表小时转义:)

So a "normal" DateTimeFormatInfo.ShortTimePattern will necessarily contain either a 'h' or a 'H', otherwise the hour will not be displayed. I think you can follow your initial idea and check for that. You can also check that the 'h' or 'H' is not escaped with a \ like in my "\hello" example because that would not represent the hour :)

这篇关于的CultureInfo&安培; DateTimeInfo:如何检查是否是24小时时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆