Export (0) Print
Expand All

Convert.ToBoolean Method (DateTime)

Calling this method always throws InvalidCastException.

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

public static bool ToBoolean (
	DateTime value
)
public static boolean ToBoolean (
	DateTime value
)
public static function ToBoolean (
	value : DateTime
) : boolean
Not applicable.

Parameters

value

A DateTime.

Return Value

This conversion is not supported. No value is returned.

Exception typeCondition

InvalidCastException

This conversion is not supported.

This method is reserved for future use.

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

public void ConvertBoolean() {
    const int year            = 1979;
    const int month            = 7;   
    const int day            = 28;
    const int hour            = 13;
    const int minute        = 26;
    const int second        = 15;
    const int millisecond    = 53;

    DateTime dateTime = new DateTime(year, month, day, hour,
                                minute, second, millisecond);
    
    bool boolVal;

    // System.InvalidCastException is always thrown.
    try {
        boolVal = System.Convert.ToBoolean(dateTime);
    }
    catch (System.InvalidCastException) {
        System.Console.WriteLine("Conversion from DateTime to " +
            "Boolean is not supported by the framework.");
    }
}

public void ConvertBoolean()
{
    final int year = 1979;
    final int month = 7;
    final int day = 28;
    final int hour = 13;
    final int minute = 26;
    final int second = 15;
    final int millisecond = 53;
    DateTime dateTime = new DateTime(year, month, day, hour, minute, second,
                            millisecond);
    boolean boolVal;

    // System.InvalidCastException is always thrown.
    try {        
        boolVal = System.Convert.ToBoolean(dateTime);
    }
    catch (System.InvalidCastException exp) {        
        System.Console.WriteLine(("Conversion from DateTime to "
            + "Boolean is not supported by the framework."));
    }
} //ConvertBoolean

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft