1 out of 3 rated this helpful - Rate this topic

Convert.ToDateTime Method

Converts a specified value to a DateTime value.

This member is overloaded. For complete information about this member, including syntax, usage, and examples, click a name in the overload list.

  Name Description
Public method Static member ToDateTime(Boolean) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(Byte) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(Char) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(DateTime) Returns the specified DateTime object; no actual conversion is performed.
Public method Static member ToDateTime(Decimal) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(Double) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(Int16) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(Int32) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(Int64) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(Object) Converts the value of the specified object to a DateTime object.
Public method Static member ToDateTime(SByte) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(Single) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(String) Converts the specified string representation of a date and time to an equivalent date and time value.
Public method Static member ToDateTime(UInt16) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(UInt32) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(UInt64) Calling this method always throws InvalidCastException.
Public method Static member ToDateTime(Object, IFormatProvider) Converts the value of the specified object to a DateTime object, using the specified culture-specific formatting information.
Public method Static member ToDateTime(String, IFormatProvider) Converts the specified string representation of a number to an equivalent date and time, using the specified culture-specific formatting information.
Top
Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
Parameter for Convert.ToDateTime method and InvaliCastException

/* Calling this method always throws System.InvalidCastException */  is applicable for most of the marameters.
This means we could never use this methods because no application would be using these logic (to get exceptions). Why they are still present in the library?
The methods where the conversion is not supported could be removed from the library.

Thanks,
Samrat

Why Do These Methods Exist?

Besides providing methods for some specialized conversions, such as to and from Base64 strings, the Convert class is intended to provide a public implementation of the IConvertible interface for each of the primitive data types in the .NET Framework. (The IConvertible implementation of each of the primitives is private.) This means that, for each primitive type, the Convert class must provide a method to convert that type to every other primitive type, even if the conversion itself is not possible. In the case of a DateTime value, most of those type conversions are either inherently impossible (such as Convert.ToDateTime(Boolean) or Convert.ToDateTime(Char)) or would require an interpretation of a primitive value that might be inaccurate (such as Convert.ToDate(Int64) or a conversion from any other integral type, which would require that the integral value be interpreted as a number of ticks). 

For more information on the Convert class and its role in type conversion for the .NET Framework, see "The Convert Class" in the Type Conversion in the .NET Framework topic at http://msdn.microsoft.com/en-us/library/98bbex99.aspx.

I hope that this helps to answer your question.

--Ron Petrusha
Common Language Runtime User Education
Microsoft Corporation