【问题标题】:.net How do I know 12/24 hour format when I know the culture Info.net 当我知道文化信息时,我如何知道 12/24 小时格式
【发布时间】:2015-06-23 09:28:06
【问题描述】:

在 .net 中,当我知道文化信息时,我如何知道 12/24 小时的时间格式?有没有简单的方法来检测指定时间格式的文化信息?

我需要知道的是文化是否应该使用 12/24 小时时间格式。我知道有些文化可以同时使用 12/24 小时时间格式。但是当我们使用 DateTime.ToShortTimeString() 时,默认的时间格式是什么?我们怎么知道默认时间格式是 12 还是 24?

【问题讨论】:

  • 一种解决方案是使用 ShortTimePattern,并检查它是否包含“tt”说明符。但它们中的大多数都不包含“tt”。这意味着我们将使用 24 小时时间格式。或者我们能知道市场是否只支持 24 小时时间格式?

标签: .net time cultureinfo


【解决方案1】:

一种方法是查看格式字符串:

CultureInfo.GetCultureInfo("cs-CZ").DateTimeFormat.ShortTimePattern.Contains("H")

如果格式字符串包含H,则表示它使用的是24 小时制。但是,如果它包含 htt - 它是 12 小时。

不过,这更像是一种肮脏的黑客攻击,而不是一个正确的解决方案(我不确定它是否适用于所有文化 - 你可能需要处理转义)。问题是 - 你为什么还要尝试确定 12/24 小时制?只需为给定的文化使用正确的格式即可。

【讨论】:

  • 它可能会起作用,但理论上没有什么可以说格式不能是"7Hours 34Minutes",其中7 是12 小时。理论上它也可以同时使用 12 和 24 小时。您应该剥离文字,然后检查两者以确保。
  • @JonHanna 是的,这就是为什么我说你可能需要处理转义。这可能不会是一个问题,但它可能。不过,剥离它们比听起来要复杂一些:D
  • 这不是那么棘手。看我的回答; new Regex(@"('.*')|("".*"")|(\\.)") 抓住了他们。
  • @JonHanna 您忘记了转义转义字符 - 例如,@"\\H"13:00 应解析为 \13 - 您将删除 \H。这就是为什么我说它比听起来有点棘手:D 而且,一旦你遇到更复杂的事情,转义会变得多么烦人...... C# 转义 + 正则表达式转义 + 日期时间格式转义......呃。
  • 不,我没有。尝试一下。 new Regex(@"('.*')|("".*"")|(\\.)").Replace(@"\\H", "") 返回 H,因为它去掉了转义字符,因此它被正确识别为 24 小时。
【解决方案2】:

可能两者兼有,因为没有什么可以阻止将 12 用于 ToShortTimeString() 和 24 用于 ToLongTimeString() 的文化,反之亦然。

但是,考虑到调用ToShortTimeString() 与调用DateTimeFormat.Format(this, "t", DateTimeFormatInfo.CurrentInfo) 相同,我们可以将其与this answer 中的方法一起使用:

[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;
}

然后致电FormatStringHourType("t") 了解是 12 小时还是 24 小时(或者两者都不是,但那会很奇怪)。

同样FormatStringHourType("T") 会告诉我们有关长时间字符串的信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2015-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-03-11
    相关资源
    最近更新 更多