我用下面的程序判断,我提示用户输入格式为2008-8-8可是用户输入2008-8,下面的也判断是正确,大家有没有好办法
try
     {
        Convert.ToDateTime(txt1.Text.Trim());//或者DateTime.Parse(txt1.Text.Trim());
                                      
     }
 catch                                     
    {
    }

解决方案 »

  1.   

    DateTime.TryParse(txt1.Text.Trim()); 
      

  2.   

                DateTime tt;
                bool b=DateTime.TryParseExact("2008-01", "yyyy-MM", null, System.Globalization.DateTimeStyles.None, out tt);
      

  3.   

                DateTime tt;
                bool b=DateTime.TryParseExact("2008-01", "yyyy-MM", null, System.Globalization.DateTimeStyles.None, out tt);
      

  4.   

    DateTime.TryParseExact()
    dateString = "5/01/2009 09:00";
    if (DateTime.TryParseExact(dateString, "M/dd/yyyy hh:mm", enUS, 
                               DateTimeStyles.None, out dateValue))
       Console.WriteLine("Converted '{0}' to {1} ({2}).", dateString, dateValue, 
                         dateValue.Kind);
    else
       Console.WriteLine("'{0}' is not in an acceptable format.", dateString);
      

  5.   

    哦。不好意思,可能是我没说清楚,我是说如果用户输入2008-8这样的就提示是错误的格式,用
    try 
        { 
            Convert.ToDateTime(“2008-8”); 
                                          
        } 
    catch                                    
        { 
        }
    不到catch 里面
      

  6.   

    为什么不用正则表达式?
    if (!System.Text.RegularExpressions.Regex.IsMatch("2008-08-8", @"^[\d]{4}-[\d]{2}-[\d]{2}$"))
        throw new ArgumentException();
      

  7.   

    小修改一下...
    if (!System.Text.RegularExpressions.Regex.IsMatch("2008-8", @"^[\d]{4}-[\d]{1,2}-[\d]{1,2}$"))
        throw new ArgumentException();
      

  8.   

    @"^[\d]{4}-[\d]{1,2}-[\d]{1,2}$"
    这样不行9999-99-99的形式也是正确的
      

  9.   

    using System;
    using System.Globalization;namespace Parse
    {
        class Class1
        {
            public static void Main(string[] args)
            {
    // Assume the current culture is en-US. 
    // The date is February 16, 1992, 12 hours, 15 minutes and 12 seconds.        string myDateTimeValue = "2/16/1992 12:15:12";
            DateTime myDateTime = DateTime.Parse(myDateTimeValue);
            Console.WriteLine("1) myDateTime       = {0}", myDateTime);// Reverse month and day to conform to a different culture.
    // The date is February 16, 1992, 12 hours, 15 minutes and 12 seconds.        IFormatProvider culture = new CultureInfo("fr-FR", true);
            string myDateTimeFrenchValue = "    16/02/1992 12:15:12";
            DateTime myDateTimeFrench =
                DateTime.Parse(myDateTimeFrenchValue,
                               culture,
                               DateTimeStyles.NoCurrentDateDefault);
            Console.WriteLine("2) myDateTimeFrench = {0}", myDateTimeFrench);
        
    // The date is February 16, 1992, 12 hours, 15 minutes and 12 seconds.        string[] expectedFormats = {"G", "g", "f" ,"F"};
            myDateTimeFrench = 
                    DateTime.ParseExact(myDateTimeFrenchValue,
                                        expectedFormats,
                                        culture,
                                        DateTimeStyles.AllowWhiteSpaces);
            Console.WriteLine("3) myDateTimeFrench = {0}", myDateTimeFrench);
            }
        }
    }
    /*
    This example yields the following results:1) myDateTime       = 2/16/1992 12:15:12 PM
    2) myDateTimeFrench = 2/16/1992 12:15:12 PM
    3) myDateTimeFrench = 2/16/1992 12:15:12 PM
    */
      

  10.   

    DateTime.TryParse(txt1.Text.Trim()); 没有采用一个参数的重载
      

  11.   

    我觉得用正则表达式if(Regex.IsMatch(str, @"^((\d{3}[1-9])|(\d{2}[1-9]\d)|(\d[1-9]\d{2})|([1-9]\d{3}))(?<connectchar>[-/. ])((1[0-2])|(0?[1-9]))(\k<connectchar>(([12]\d)|(3[01])|(0?[1-9]))$"))?)
    {
           throw new ArgumentException();}
    其中str是日期字符串
      

  12.   

    用DateTime.TryParse  都可以
      

  13.   

    if (!System.Text.RegularExpressions.Regex.IsMatch("2008-8", @"^[\d]{4}-[\d]{1,2}-[\d]{1,2}$"))
        throw new ArgumentException();
    赞成 
      

  14.   

    DateTime.TryParse可以判断2008-8格式错误嘛。怎么判断啊,它不是如果错误返回一个minivalue么,怎么判断是格式是不是错误啊?
      

  15.   

    按楼主的要求只匹配yyyy-M-d格式,年份为1600-9999,带闰年判断if (System.Text.RegularExpressions.Regex.IsMatch(yourStr, @"^(?:(?:(?:(?:(?:1[6-9]|[2-9][0-9])?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))-(?:2-29))|(?:(?:(?:1[6-9]|[2-9][0-9])?[0-9]{2})-(?:(?:(?:[13578]|1[02])-31)|(?:(?:[13-9]|1[0-2])-(29|30))|(?:(?:[1-9])|(?:1[0-2]))-(?:[1-9]|1[0-9]|2[0-8]))))$"))
        richTextBox2.Text += "符合" + "\n";
    else
        richTextBox2.Text += "不符合" + "\n";