views:

269

answers:

2

How can i do a isDate() control on a date like 16/01/2008 in vb.net?

+4  A: 

To check if 16/01/2008 is a valid date, you do:

Dim result As Date
If Date.TryParse("16/01/2008", result) Then
     'The date is valid '
End If

Now this will use the current culture set. If you want to validate it against a specific culture, you can do it like this (example with french culture):

If Date.TryParse("16/01/2008", Globalization.CultureInfo.GetCultureInfo("fr-FR"), _
                 Globalization.DateTimeStyles.None, result) Then
    'The date is valid '
End If
Meta-Knight
+2  A: 

If the date format in your input data is fixed to month/day/year and does not depend on the current culture, you should use DateTime.TryParseExact:

Public Shared Function IsValidInputDate(ByVal str As String) As Boolean
    Dim dt as DateTime
    Return DateTime.TryParseExact(str, "d/M/yyyy", Nothing, Globalization.DateTimeStyles.None, dt)
End Function
Hans Passant