C# Convert ToBoolean(DateTime)
Description
Convert ToBoolean(DateTime)
calling this method always
throws InvalidCastException.
Syntax
Convert.ToBoolean(DateTime)
has the following syntax.
public static bool ToBoolean(
DateTime value
)
Parameters
Convert.ToBoolean(DateTime)
has the following parameters.
value
- The date and time value to convert.
Returns
Convert.ToBoolean(DateTime)
method returns This conversion is not supported. No value is returned.
Example
The following example demonstrates that an attempt to convert a DateTime value to a Boolean type throws InvalidCastException.
// w ww.ja v a2 s .c om
using System;
public class MainClass{
public static void Main(String[] argv){
DateTime dateTime = DateTime.Now;
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 .NET Framework.");
}
}
}
The code above generates the following result.