There is no need to verify the format because the "parse" methods will do this for you. the parse will compare all date format strings in DateTimeFormatInfo
against the string you pass to the method. The parse-exact method will only compare the specified string against the data format strings you pass to the method.
Imports System.Globalization
Module Sample
Public Function IsValidDateString1(ByVal s As String) As Boolean
Return Date.TryParseExact(s, "dd/MM/yy", CultureInfo.InvariantCulture, DateTimeStyles.None, Nothing)
End Function
Public Function IsValidDateString2(ByVal s As String) As Boolean
Static _dateFormats() As String = New String() {"dd/MM/yy", "d/M/yy", "d/M/yyyy"}
Return Date.TryParseExact(s, _dateFormats, CultureInfo.InvariantCulture, DateTimeStyles.None, Nothing)
End Function
Public Sub Main()
Debug.WriteLine("single")
Debug.WriteLine(IsValidDateString1("31/12/2001")) 'wrong format
Debug.WriteLine(IsValidDateString1("31/12/01"))
Debug.WriteLine(IsValidDateString1("29/2/08")) '<-be careful
Debug.WriteLine(IsValidDateString1("29/ 2/08")) '<-be careful
Debug.WriteLine(IsValidDateString1("29/02/08"))
Debug.WriteLine(IsValidDateString1("29/02/09")) 'invalide date
Debug.WriteLine("multiple")
Debug.WriteLine(IsValidDateString2("31/12/2001"))
Debug.WriteLine(IsValidDateString2("31/12/01"))
Debug.WriteLine(IsValidDateString2("29/2/08"))
Debug.WriteLine(IsValidDateString2("29/ 2/08")) '<-be careful
Debug.WriteLine(IsValidDateString2("29/02/08"))
Debug.WriteLine(IsValidDateString2("29/02/09")) 'invalid date
End Sub
End Module