如何使用C#DateTime.TryParse(最简单的方法)

时间:2014-01-08 18:13:24

标签: c# datetime

我在win7 pt-BR机器上使用visual studio 2012,但需要此代码才能在Win2003ServerR2 en-US服务器上运行。

获得以下错误:

  

字符串未被识别为有效的DateTime。   System.Collections.ListDictionaryInternal mscorlib at   System.DateTimeParse.ParseExact(String s,String format,   DateTimeFormatInfo dtfi,DateTimeStyles style)at   System.DateTime.ParseExact(String s,String format,IFormatProvider   提供者)在AppCombustivel.Service1.TabelaConsumos()   System.DateTime ParseExact(System.String,System.String,   System.Globalization.DateTimeFormatInfo,   System.Globalization.DateTimeStyles)

我的ShortDate服务器配置:

yyyy/MM/dd

我的代码:

string MyString: return the result of "SELECT MAX(Data_Column) FROM Table_Data T" (the last date registered in my BD)

MyString可以像:

13/01/2013 00:00:00

01/15/2013 00:00:00

2013/02/30 00:00:00,我不知道,它取决于每个操作系统的语言和短日期格式。 我只需要MyString的日期,来自MyString的时间信息并不重要 * /

DateTime dateIwillUse;

    if (DateTime.TryParse("20/01/2013", out dateIwillUse))
            {
                Console.WriteLine("This System uses short Date in format: dd/MM/yyyy. Parsing in correct format to dateIwillUse:");
                dateIwillUse = DateTime.ParseExact(MyString.Substring(0, 10), "dd/MM/yyyy", CultureInfo.CurrentCulture);
        //using the variable dateIwillUse with dateIwillUse.ToString("yyyy-MM-dd"), after correctly parsed
            }
            else if (DateTime.TryParse("01/20/2013", out dateIwillUse))
            {
                    Console.WriteLine("This system uses short date in format: MM/dd/yyyy.Parsing in correct format to dateIwillUse:");
                    dateIwillUse = DateTime.ParseExact(MyString.Substring(0, 10), "MM/dd/yyyy", CultureInfo.CurrentCulture );
                //using the variable dateIwillUse with dateIwillUse.ToString("yyyy-MM-dd"), after correctly parsed
            }
            else if (DateTime.TryParse("2013/01/20", out dateIwillUse))
            {
                Console.WriteLine("This system uses short date in format: yyyy/MM/dd .Parsing in correct format to dateIwillUse:");
                dateIwillUse = DateTime.ParseExact(MyString.Substring(0, 10), "yyyy/MM/dd", CultureInfo.CurrentCulture);![enter image description here][2]
            //using the variable dateIwillUse with dateIwillUse.ToString("yyyy-MM-dd"), after correctly parsed
            }
            else if (DateTime.TryParse("01/20/2013", out dateIwillUse))
            {
                Console.WriteLine("This system uses short date in format: MM/dd/yyyy .Parsing in correct format to dateIwillUse:");
                dateIwillUse = DateTime.ParseExact(MyString.Substring(0, 10), "MM/dd/yyyy", CultureInfo.CurrentCulture);
                //using the variable dateIwillUse with dateIwillUse.ToString("yyyy-MM-dd"), after correctly parsed
            } else 
{ 
    Console.WriteLine("Cannot determine the format. Is not MM/dd/yyyy, neither yyyy/MM/dd, or dd/MM/yyyy");
}

2 个答案:

答案 0 :(得分:4)

为什么在检索sql时不强制使用特定的日期格式?这样你的代码就会知道字符串的格式。没有理由编写这么多的解析器。

http://anubhavg.wordpress.com/2009/06/11/how-to-format-datetime-date-in-sql-server-2005/

修改:添加了更多信息

只有您的数据库知道日期的正确格式。如果您尝试使用不同的解析器,则日期01-05-2013是2013年1月5日或2013年5月1日,这两个日期都是有效日期并将进行解析,但它们具有非常不同的含义,请小心。

答案 1 :(得分:0)

DateTime.Parse有一个重载,允许您根据日期的格式进行解析。 http://msdn.microsoft.com/en-us/library/system.datetime.parse(v=vs.110).aspx#Parse2_Example

相关问题