【问题标题】:Get custom datetime format from standard format从标准格式获取自定义日期时间格式
【发布时间】:2013-08-06 19:56:25
【问题描述】:

在我的脚本中,我收到了标准格式,例如“D”、“f”、“R”或其他格式。根据 MSDN,这是标准日期时间格式。

考虑到用户当前的文化,我想得到这个标准格式的自定义格式。

例如,假设我的用户来自法国 (fr-FR):

"d" = "dd/MM/yyyy"

"D" = "dddd d MMMM yyyy"

"F" = "dddd d MMMM yyyy HH:mm:ss"

【问题讨论】:

    标签: c# datetime datetime-format


    【解决方案1】:

    您需要该格式的char 版本,但您可以这样做:

    CultureInfo culture = //get your culture
    var patterns = culture.DateTimeFormat.GetAllDateTimePatterns(yourFormatChar);
    

    【讨论】:

    • 简单干净!它就像一个魅力!而且我想我总是取第一个元素,因为 GetAllDateTimePatterns() 返回一个 string[]?.
    • @NLemay 这取决于你是否想要其他的,我认为某些格式字符串返回多个。但是我认为采用第一个元素是安全的。
    • 好的!不确定在哪种情况下我可以获得多个自定义格式以获得独特的标准格式。使用 DateTime.toString() 时,C# 将如何处理这个问题?但无论如何,第一个可能是好的!
    • @NLemay 我真的不知道,我会做一些测试,我会回来的。
    • @NLemay LINQPad 说可以返回更多结果的是 d、D、f、F、g、G、t、T 和 U。(至少在 en-UK 中)。但是,第一个是最常见的,所以我总是选择第一个。
    【解决方案2】:

    这里有一些获取模式的代码:

    var c = new CultureInfo("fr-FR");
    Console.WriteLine(c.DateTimeFormat.LongDatePattern);
    Console.WriteLine(c.DateTimeFormat.ShortDatePattern);
    Console.WriteLine(c.DateTimeFormat.FullDateTimePattern);
    

    控制台应用程序的结果如下

    dddd d MMMM yyyy
    dd/MM/yyyy
    dddd d MMMM yyyy HH:mm:ss
    

    【讨论】:

    • 这不能回答问题:他需要它来处理格式字符串。
    • 这个例子让他能够得到格式,他只需要修改代码以供他使用。
    • 感谢您的时间。但就像我说的那样,蒂姆 S. 和 D 斯坦利,我正在寻找一个更简单的解决方案(尽可能少的代码)。抱歉,我的问题本来可以更清楚的。
    • 是的,我同意 It'sNotALie 的回答比我和其他人的要好。再见
    【解决方案3】:
    DateTimeFormatInfo dtf = CultureInfo.CurrentCulture.DateTimeFormat;
    switch (standardFormat)
    {
        case "d":
            return dtf.ShortDatePattern;
        case "D":
            return dtf.LongDatePattern;
        case "F":
            return dtf.FullDateTimePattern;
        // add other standard formatters
        default:
            throw new ArgumentException("Say what?", "standardFormat");
    }
    

    standard formatter documentation 说明您需要查找哪些属性。

    【讨论】:

    • 这行得通(从其他答案改进)但编写、调试和维护会很痛苦。
    • 感谢您的回答。我考虑过这个解决方案,但我正在寻找一种更清洁的方法来做到这一点。就像 It'sNotALie 说的那样,这段代码维护起来很痛苦。抱歉,我的问题不够清楚。
    【解决方案4】:

    使用当前线程的DateTimeFormat 的各种属性(或UI Thread,以适当者为准):

    "d" = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern 
    
    "D" = Thread.CurrentThread.CurrentCulture.DateTimeFormat.LongDatePattern 
    
    "F" = Thread.CurrentThread.CurrentCulture.DateTimeFormat.FullDateTimePattern
    

    【讨论】:

    • @It'sNotALie。以什么方式?这三个属性分别代表“d”、“D”和“F”的标准格式字符串——它甚至在the documentation中这么说。
    • 是的,但是对于额外的格式字符串会很痛苦。如果你打错了,你需要打开互联网,去文档,寻找正确的,修复你的代码。然后为任何其他人再做一次。
    • 我同意 It'sNotALie。我更喜欢让 C# 来处理这项工作,如果可以的话,不要在我的应用程序中添加太多代码。不过还是谢谢你的回答!
    【解决方案5】:

    一些反思可能会确保您获得内部使用的相同扩展格式字符串:

    string GetRealFormat(string format, DateTimeFormatInfo dtfi)
    {
        MethodInfo method = Type.GetType("System.DateTimeFormat")
            .GetMethod("GetRealFormat", 
                       BindingFlags.Static | BindingFlags.NonPublic);
    
        return method.Invoke(null, new object[] { format, dtfi }) as string;
    }
    
    string format = GetRealFormat("d", DateTimeFormatInfo.CurrentInfo) // dd.MM.yyyy
    

    【讨论】:

    • 如果我理解得很好,用这个解决方案,我肯定会得到最好的匹配而不是字符串[]?这不是最简单的,但肯定是一个整洁而漂亮的答案!谢谢!
    • 此解决方案在 .netstandart 中不起作用:“System.String”类型的对象无法转换为“System.ReadOnlySpan`1[System.Char]”类型
    猜你喜欢
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多