时间格式为24小时格式吗? [英] Is the time format in 24-hour format?

查看:223
本文介绍了时间格式为24小时格式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个以字符串形式传递给我的时间格式字符串。它可以是标准字符串,例如 t,但可以是自定义字符串(包含 HH等)。我如何确定如果显示为12小时或24小时格式? (取决于文化和一切……)

I get a time format string passed to me in a string. It could be a standard string like "t", but could be a custom one (containing "HH", etc). How can I find out, that if displayed it'll be a 12-hour or 24-hour format? (depending on culture and everything...)

推荐答案

最简单的方法是尝试约会,然后可以检查日期给定字符串中的数字,您只会期望那样。例如。 2000-01-01T19:00:00Z 是否导致包含 7 9 或两者皆有。

The easiest way is to try a date that you can then examine for a given number in the string, that you'd only expect that way. E.g. whether 2000-01-01T19:00:00Z resulted in a string containing a 7 or a 9 or both.

在遇到像这样的奇怪字符串时也要确定此有效字符串将将您弄乱79' ,然后在获得给定文化信息的完整版本(如果需要)之后,需要检查字符串。

To be certain in the face of even strange strings like "'This valid string will mess you up 79'", then you need to examine the string, after obtaining the full version for the given culture info if needed.

作为 CultureInfo DateTimeFormatInfo 的扩展方法,以下内容也很有意义。

The following would also make sense as an extension method on CultureInfo or DateTimeFormatInfo.

在实践中,您可以通过将通常只包含日期信息的那些标准格式放入立即返回 None 的组中来简化操作,但是我决定抓住

In practice, you can simplify by putting those standard formats that normally include only date-info in the group that returns None immediately, but I decided to catch even strangeness there:

[Flags]
public enum HourRepType
{
  None = 0,
  Twelve = 1,
  TwentyFour = 2,
  Both = Twelve | TwentyFour
}
public static HourRepType FormatStringHourType(string format, CultureInfo culture = null)
{
  if(string.IsNullOrEmpty(format))
    format = "G";//null or empty is treated as general, long time.
  if(culture == null)
    culture = CultureInfo.CurrentCulture;//allow null as a shortcut for this
  if(format.Length == 1)
    switch(format)
    {
      case "O": case "o": case "R": case "r": case "s": case "u":
        return HourRepType.TwentyFour;//always the case for these formats.
      case "m": case "M": case "y": case "Y":
        return HourRepType.None;//always the case for these formats.
      case "d":
          return CustomFormatStringHourType(culture.DateTimeFormat.ShortDatePattern);
      case "D":
        return CustomFormatStringHourType(culture.DateTimeFormat.LongDatePattern);
      case "f":
        return CustomFormatStringHourType(culture.DateTimeFormat.LongDatePattern + " " + culture.DateTimeFormat.ShortTimePattern);
      case "F":
        return CustomFormatStringHourType(culture.DateTimeFormat.FullDateTimePattern);
      case "g":
        return CustomFormatStringHourType(culture.DateTimeFormat.ShortDatePattern + " " + culture.DateTimeFormat.ShortTimePattern);
      case "G":
        return CustomFormatStringHourType(culture.DateTimeFormat.ShortDatePattern + " " + culture.DateTimeFormat.LongTimePattern);
      case "t":
        return CustomFormatStringHourType(culture.DateTimeFormat.ShortTimePattern);
      case "T":
        return CustomFormatStringHourType(culture.DateTimeFormat.LongTimePattern);
      default:
        throw new FormatException();
    }
  return CustomFormatStringHourType(format);
}
private static HourRepType CustomFormatStringHourType(string format)
{
  format = new Regex(@"('.*')|("".*"")|(\\.)").Replace(format, "");//remove literals
  if(format.Contains("H"))
    return format.Contains("h") ? HourRepType.Both : HourRepType.TwentyFour;
  return  format.Contains("h") ? HourRepType.Twelve : HourRepType.None;
}

这篇关于时间格式为24小时格式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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