如提
验证日期的格式 dd/mm/yyyy
验证日期大于或者小于某个值。

解决方案 »

  1.   

    先验证日期的格式,用正则表达式
    using System.Text.RegularExpressions;
    public static IsValidDate( string strInput )
    {
    Regex DateRegex =new Regex(@"(3[0,1]|[1,2][0-9]|0?[1-9])/(1[0-2]|0?[1-9])/(((19)|(20))[0-9][0-9]));
      if( DateRegex.IsMatch(strInput) )
         return ture;
      return false;
    }
    在用DateTime的里的比较函数比较。
      

  2.   

    [VB.NET]
    imports System.Text.RegularExpressionsPublic Static Function IsValidDate(Byval strInput As String) As Boolean
       Dim DateRegex As Regex = New Regex(@"(3[0,1]|[1,2][0-9]|0?[1-9])/(1[0-2]|0?[1-9])/(((19)|(20))[0-9][0-9]))
       If DateRegex.IsMatch( strInput ) then
          Return True
       End If
       Return False
    End Function
    '再验证符合日期格式之后。
    '验证是否大于或者小于某个要求日期。
    '小于零 t1 小于 t2。 
    '零 t1 等于 t2。 
    '大于零 t1 大于 t2。 
    Public Static Function CompareDate( Byval t1 As String, Byval t2 As String ) As Integer
        Dim Date1 As DateTime = New DateTime(t1)
        Dim Date2 As DateTime = New DateTime(t2)
        Return DateTime.Compare( Date1, Date2 )
    End Function