Use of IsDate() function in VB.NET

IsDate() function returns True if Expression is of the Date Data Type or can be converted to Date; otherwise, it returns False.
  • 1970

If the value already exists in a DateTime variable, then we can assume that it is a valid value. So we start with the assumption that the user has the value in a string or something easily converted to a string. IsDate returns True if Expression is of the Date Data Type or can be converted to Date; otherwise, it returns False.

An alternate variation removes some of the cohesion from the routine, but you could also make resultDate variable in this an 'out' argument and make it that much easier for the caller to get a DateTime value once it's been confirmed that the value is indeed a date. The Date data type holds both a date value and a time value. IsDate returns True if Expression represents a valid date, a valid time, or a valid date and time.

Example:

Public
 Class Sample
    
Public Shared Sub Main()
        
Dim Data1 As String = Nothing
        
Dim Output As New System.Text.StringBuilder

        
Output.AppendLine(String.Format( _
           
"IsDate({0}) ... {1}", Data1, IsDate(Data1)))
        
Output.AppendLine()

        
Data1 = "-11.1231"
        
Output.AppendLine(String.Format( _
           
"IsDate({0}) ... {1}", Data1, IsDate(Data1)))
        
Output.AppendLine()

        
Data1 = "October 21, 1987"
        
Output.AppendLine(String.Format( _
           
"IsDate({0}) ... {1}", Data1, IsDate(Data1)))

        
Console.WriteLine(Output.ToString())
        
Console.ReadLine()

    
End Sub

End
 Class

Output:

date.gif

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.