Convert.ToBoolean Method (DateTime)

 

Calling this method always throws InvalidCastException.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

Public Shared Function ToBoolean (
	value As Date
) As Boolean

Parameters

value
Type: System.DateTime

The date and time value to convert.

Return Value

Type: System.Boolean

This conversion is not supported. No value is returned.

Exception Condition
InvalidCastException

This conversion is not supported.

The following example demonstrates that an attempt to convert a DateTime value to a Boolean type throws InvalidCastException.

Public Sub ConvertBoolean()
    Const year As Integer = 1979
    Const month As Integer = 7
    Const day As Integer = 28
    Const hour As Integer = 13
    Const minute As Integer = 26
    Const second As Integer = 15
    Const millisecond As Integer = 53

    Dim dateTime As New DateTime(year, month, day, hour, minute, _
                                 second, millisecond)

    Dim boolVal As Boolean

    ' System.InvalidCastException is always thrown.
    Try
        boolVal = System.Convert.ToBoolean(dateTime)
    Catch exception As System.InvalidCastException
        System.Console.WriteLine("Conversion from DateTime to " + _
                "Boolean is not supported by the .NET Framework.")
    End Try
End Sub

.NET Framework
Available since 1.1
Return to top
Show: