解析DateTime格式得到格式字符串 [英] Parsing the DateTime format to get Format String

查看:200
本文介绍了解析DateTime格式得到格式字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够从日期字符串得到的格式字符串

I would like to be able to get the format string from a DateTime string.

例如

2012年12月8日15时零零分00秒=>YYYY-MM-DD HH:MM:SS

"2012-12-08 15:00:00" => "yyyy-MM-dd HH:mm:ss"

2013/30/01 16:00=>YYYY / DD / MM HH:MM

"2013/30/01 16:00" => "yyyy/dd/MM HH:mm"

这可能吗?

推荐答案

这将是很难做到这一点在一个完全普遍的方​​式,但其中一个方案是提取相关 的DateTimeFormatInfo > 请将CultureInfo.DateTimeFormat ),从( LongDatePattern ,<$ C $提取特定的文化模式C> LongTimePattern 等),结合图案适当地在某些情况下(如 ShortDatePattern 空间 ShortTimePattern ),然后使用 DateTime.TryParseExact 依次尝试每个模式 - 记住每次还指定文化,以处理日期分隔等适当

It would be very hard to do this in a completely general way, but one option would be to extract the relevant DateTimeFormatInfo that you're interested in (using CultureInfo.DateTimeFormat), extract the culture-specific patterns from that (LongDatePattern, LongTimePattern etc), combine the patterns appropriately in some cases (e.g. ShortDatePattern space ShortTimePattern) and then try each pattern in turn using DateTime.TryParseExact - remembering to still specify the culture each time in order to handle date separators etc appropriately.

DateTime.TryParseExact 收益真正,你知道你已经得到的的格局,这将解析给定文本

When DateTime.TryParseExact returns true, you know you've got a pattern which will parse the given text.

示例代码 - 包括显示在你期望它的工作,但它没有一个例子:

Sample code - including showing an example where you'd expect it to work but it doesn't:

using System;
using System.Collections.Generic;
using System.Globalization;

class Test
{
    static void Main()        
    {
        var us = new CultureInfo("en-US");
        var uk = new CultureInfo("en-GB");
        string text = "07/06/2013 11:22:11";

        // This one fails, as there's no appropriate time format
        Console.WriteLine(GuessPattern(text, us));
        // This one prints dd/MM/yyyy HH:mm:ss
        Console.WriteLine(GuessPattern(text, uk));
    }

    static string GuessPattern(string text, CultureInfo culture)
    {
        foreach (var pattern in GetDateTimePatterns(culture))
        {
            DateTime ignored;
            if (DateTime.TryParseExact(text, pattern, culture,
                                       DateTimeStyles.None, out ignored))
            {
                return pattern;
            }
        }
        return null;
    }

    static IList<string> GetDateTimePatterns(CultureInfo culture)
    {
        var info = culture.DateTimeFormat;
        return new string[]
        {
            info.FullDateTimePattern,
            info.LongDatePattern,
            info.LongTimePattern,
            info.ShortDatePattern,
            info.ShortTimePattern,
            info.MonthDayPattern,
            info.ShortDatePattern + " " + info.LongTimePattern,
            info.ShortDatePattern + " " + info.ShortTimePattern,
            info.YearMonthPattern
            // Consider the sortable pattern, ISO-8601 etc
        };        
    }
} 

您可能会硬编码了一些额外的日期以及你期望的工作时间格式

You could potentially hard-code some "extra" date and time formats which you expect to work.

编辑:要处理歧义,你可以很容易地使 GuessPattern 返回的IEnumerable<串> ,而不是一个字符串:

To handle ambiguity, you could easily make GuessPattern return an IEnumerable<string> instead of a single string:

static IEnumerable<string> GuessPatterns(string text, CultureInfo culture)
{
    DateTime ignored;
    return GetDateTimePatterns(culture)
        .Where(pattern => DateTime.TryParseExact(text, pattern, culture,
                                             DateTimeStyles.None, out ignored))
    }
}

这篇关于解析DateTime格式得到格式字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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