6

我正在申请。该应用程序使用日期格式,例如 "2012-11-21 15:22:35".
我已经知道格式是"yyyy-MM-dd HH:mm:ss". 但是如何以编程方式找到任意输入字符串的日期和时间格式?有没有办法做到这一点?

4

4 回答 4

10

这可能会对您有所帮助..(在数组中,添加更多格式以进行检查)

string[] formats = {"M/d/yyyy", "MM/dd/yyyy",                                    
                            "d/M/yyyy", "dd/MM/yyyy", 
                            "yyyy/M/d", "yyyy/MM/dd",
                            "M-d-yyyy", "MM-dd-yyyy",                                    
                            "d-M-yyyy", "dd-MM-yyyy", 
                            "yyyy-M-d", "yyyy-MM-dd",
                            "M.d.yyyy", "MM.dd.yyyy",                                    
                            "d.M.yyyy", "dd.MM.yyyy", 
                            "yyyy.M.d", "yyyy.MM.dd",
                            "M,d,yyyy", "MM,dd,yyyy",                                    
                            "d,M,yyyy", "dd,MM,yyyy", 
                            "yyyy,M,d", "yyyy,MM,dd",
                            "M d yyyy", "MM dd yyyy",                                    
                            "d M yyyy", "dd MM yyyy", 
                            "yyyy M d", "yyyy MM dd"
                           };

        DateTime dateValue;

        foreach (string dateStringFormat in formats)
        {
            if (DateTime.TryParseExact(strDateTime, dateStringFormat,
                                       CultureInfo.InvariantCulture,
                                       DateTimeStyles.None,
                                       out dateValue))
                //Console.WriteLine("Converted '{0}' to {1}.", dateStringFormat, dateValue.ToString("yyyy-MM-dd"));                
                Console.WriteLine( dateStringFormat);
        }
于 2014-02-12T19:34:51.143 回答
5

我认为您会遇到月份值 <= 12 的日期字符串的问题,因为这意味着该字符串的格式可能是"yyyy-MM-dd""yyyy-dd-MM"

除非您在解析器中设置首选项,否则无法知道哪个是正确的。

例如: 2012/08/07-“这可能是七月还是八月?”

你可以暴力破解它,并希望有一个符合格式的 CultureInfo

 var datestring = "2012-10-05 12:00:03";
 DateTime time;
 var matchingCulture = CultureInfo.GetCultures(CultureTypes.AllCultures).FirstOrDefault(ci => DateTime.TryParse(datestring, ci, DateTimeStyles.None, out time))
于 2012-11-22T13:51:55.223 回答
2

如果您想识别格式,几乎没有办法 100% 确定地做到这一点。但是,您可能想要浏览 givenCultureInfoTryParse它们支持的所有格式。不幸的是,这可能会产生不正确的结果,因为无法通过以下方式判断年、月和日:

2012 年 10 月 11 日

根据您的文化偏见,您可以将其解释为 2012 年 10 月 11 日;2012 年 11 月 10 日或 2010 年 11 月 12 日。

你还没有提到你想用这个日期做什么,所以我会给你常规的最佳实践:

  1. 如果要在应用程序的不同模块之间传输日期,请使用不变的日期格式。
  2. 如果您需要格式化日期和时间,请使用给定文化的默认日期格式。
  3. 谈到默认日期格式,如果您想接受最终用户输入的日期,您可能希望以文化的默认格式解析自由格式输入,或者您可以创建(或使用预先存在的)日期时间选择器控件。后一种方法是优选的。

广告 1. 要转换为不变的日期和时间格式,请使用:

DateTime now = DateTime.UtcNow;
string formatted = now.ToUniversalTime.ToString(CultureInfo.InvariantCulture);

或(转换为类似 ISO8601的格式):

string formatted = now.ToString("u");

同样,您可以从不变格式解析 DateTime:

DateTime result;
string source = "11/20/2012 11:22:33";
if (DateTime.TryParse(source, CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out result))
{
  // do something with result
}

或(通用格式不需要 CultureInfo):

DateTime result;
string source = "2012-11-20 11:22:33Z";
if (DateTime.TryParse(source, out result))
{
  // do something
}

广告 2 和 3。特定文化的格式化和解析类似于格式化和解析不变日期,但您需要将 InvariantCulture 替换为检测到的特定实例,例如CultureInfo.CurrentCulture.

根据应用程序的类型,您可能希望使用专用的日历控件,即jQuery UI Datepicker

于 2012-11-22T18:09:01.930 回答
-1

使用以下代码:

字符串 sysFormat = CultureInfo.CurrentCulture.DateTimeFormat;

于 2012-11-22T13:50:45.593 回答