首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >.net当我知道区域性信息时,如何知道12/24小时格式

.net当我知道区域性信息时,如何知道12/24小时格式
EN

Stack Overflow用户
提问于 2015-06-23 17:28:06
回答 2查看 516关注 0票数 1

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

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

EN

回答 2

Stack Overflow用户

发布于 2015-06-23 17:37:39

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

代码语言:javascript
复制
CultureInfo.GetCultureInfo("cs-CZ").DateTimeFormat.ShortTimePattern.Contains("H")

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

然而,这更像是一种肮脏的黑客手段,而不是一个适当的解决方案(我不确定它是否适用于所有文化-您可能需要处理转义)。问题是--为什么你要尝试确定12/24小时?只需为给定的区域性使用适当的格式,并完成它。

票数 1
EN

Stack Overflow用户

发布于 2015-06-23 17:39:39

它可以两者兼而有之,因为没有什么可以阻止文化对ToShortTimeString()使用12,对ToLongTimeString()使用24,反之亦然。

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

代码语言:javascript
复制
[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")会告诉我们关于长时间字符串的信息。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30998953

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档