Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用当前区域性定义的12小时或24小时格式仅获取DateTime之后的小时数_C#_.net_Culture_Datetime Format - Fatal编程技术网

C# 使用当前区域性定义的12小时或24小时格式仅获取DateTime之后的小时数

C# 使用当前区域性定义的12小时或24小时格式仅获取DateTime之后的小时数,c#,.net,culture,datetime-format,C#,.net,Culture,Datetime Format,.Net具有DateTime的内置ToSortTimeString()函数,该函数使用CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern格式。它为我们返回类似这样的内容:“下午5:00”。对于像de de这样的24小时文化,它将返回“17:00” 我想要的是一种只返回一个小时的方式(在上面的例子中是“下午5点”和“17点”),这种方式适用于所有文化。最好/最干净的方法是什么 谢谢 您可以使用DateTime.ToString()并

.Net具有DateTime的内置ToSortTimeString()函数,该函数使用CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern格式。它为我们返回类似这样的内容:“下午5:00”。对于像de de这样的24小时文化,它将返回“17:00”

我想要的是一种只返回一个小时的方式(在上面的例子中是“下午5点”和“17点”),这种方式适用于所有文化。最好/最干净的方法是什么


谢谢

您可以使用DateTime.ToString()并提供所需的格式作为参数。

我将检查CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern是否包含“h”、“hh”、“h”、“t”或“tt”,以及它们的顺序,然后从中构建您自己的自定义格式字符串

var culture = CultureInfo.CurrentCulture; bool uses24HourClock = string.IsNullOrEmpty(culture.DateTimeFormat.AMDesignator); var dt = DateTime.Now; string formatString = uses24HourClock ? "HH" : "h tt"; Console.WriteLine(dt.ToString(formatString, culture)); e、 g

  • en US:将“h:mm tt”映射到“h tt”
  • ja JP:将“H:mm”映射到“H”
  • fr fr:将“HH:mm”映射到“HH”
然后使用.ToString(),传入生成的字符串

示例代码-这基本上去掉了不是t、t、h、h和多个空格的所有内容。但是,正如下面指出的,仅仅一串“H”就可能失败

string full = System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
string sh = String.Empty;
for (int k = 0; k < full.Length; k++)
{
    char i = full[k];
    if (i == 'h' || i == 'H' || i == 't' || i == 'T' || (i == ' ' && (sh.Length == 0 || sh[sh.Length - 1] != ' ')))
    {
        sh = sh + i;
    }
}
if (sh.Length == 1)
{
  sh = sh + ' ';
  string rtnVal = DateTime.Now.ToString(sh);
  return rtnVal.Substring(0, rtnVal.Length - 1);
{
else
{
    return DateTime.Now.ToString(sh);
}
string full=System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.ShortTimePattern;
string sh=string.Empty;
for(int k=0;k
    public static class DateTimeStaticExtensions
    {
        private static int GetDesignatorIndex(CultureInfo info)
        {
            if (info.DateTimeFormat
                .ShortTimePattern.StartsWith("tt"))
            {
                return 0;
            }
            else if (info.DateTimeFormat
                .ShortTimePattern.EndsWith("tt"))
            {
                return 1;
            }
            else
            {
                return -1;
            }
        }

        private static string GetFormattedString(int hour, 
            CultureInfo info)
        {
            string designator = (hour > 12 ? 
                info.DateTimeFormat.PMDesignator : 
                info.DateTimeFormat.AMDesignator);

            if (designator != "")
            {
                switch (GetDesignatorIndex(info))
                {
                    case 0:
                        return string.Format("{0} {1}",
                            designator, 
                            (hour > 12 ? 
                                (hour - 12).ToString() : 
                                hour.ToString()));
                    case 1:
                        return string.Format("{0} {1}",
                            (hour > 12 ? 
                                (hour - 12).ToString() :
                                hour.ToString()), 
                            designator);
                    default:
                        return hour.ToString();
                }
            }
            else
            {
                return hour.ToString();
            }
        }

        public static string ToTimeString(this DateTime target, 
            CultureInfo info)
        {
            return GetFormattedString(target.Hour, info);
        }

        public static string ToTimeString(this DateTime target)
        {
            return GetFormattedString(target.Hour, 
                CultureInfo.CurrentCulture);
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            var dt = new DateTime(2010, 6, 10, 6, 0, 0, 0);

            CultureInfo[] cultures = 
                CultureInfo.GetCultures(CultureTypes.SpecificCultures);
            foreach (CultureInfo culture in cultures)
            {
                Console.WriteLine(
                    "{0}: {1} ({2}, {3}) [Sample AM: {4} / Sample PM: {5}",
                    culture.Name, culture.DateTimeFormat.ShortTimePattern,
                    (culture.DateTimeFormat.AMDesignator == "" ? 
                        "[No AM]": 
                        culture.DateTimeFormat.AMDesignator),
                    (culture.DateTimeFormat.PMDesignator == "" ? 
                        "[No PM]": 
                        culture.DateTimeFormat.PMDesignator),
                    dt.ToTimeString(culture),  // AM sample
                    dt.AddHours(12).ToTimeString(culture) // PM sample
                    );
            }

            // pause program execution to review results...
            Console.WriteLine("Press enter to exit");
            Console.ReadLine();
        }
    }