Convert.ToDateTime Method (Object)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Syntax
'Declaration
Public Shared Function ToDateTime ( _
value As Object _
) As DateTime
public static DateTime ToDateTime(
Object value
)
Parameters
- value
Type: System.Object
An Object that implements the IConvertible interface or nulla null reference (Nothing in Visual Basic).
Return Value
Type: System.DateTime
A DateTime equivalent to the value of value.
-or-
A DateTime equivalent to DateTime.MinValue if value is nulla null reference (Nothing in Visual Basic).
Exceptions
Exception | Condition |
---|---|
FormatException | value is not a valid DateTime value. |
InvalidCastException | value does not implement IConvertible. -or- The conversion is not supported. |
Remarks
For the conversion to succeed, the runtime type of the value parameter must be either a DateTime or a String, or value must be nulla null reference (Nothing in Visual Basic). Otherwise, the method throws an InvalidCastException. In addition, if value is a string, it must contain a valid representation of a date and time value in the current culture or a FormatException is thrown.
The return value is the result of invoking the IConvertible.ToDateTime method of the underlying type of value.
Examples
The following example calls the ToDateTime(Object) method with a variety of Object variables.
Module Example
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
' Try converting an integer.
Dim number As Integer = 16352
ConvertToDateTime(outputBlock, number)
' Convert a null.
Dim obj As Object = Nothing
ConvertToDateTime(outputBlock, obj)
' Convert a non-date string.
Dim nonDateString As String = "monthly"
ConvertToDateTime(outputBlock, nonDateString)
' Try to convert various dates.
Dim dateString As String
dateString = "05/01/1996"
ConvertToDateTime(outputBlock, dateString)
dateString = "Tue Apr 28, 2009"
ConvertToDateTime(outputBlock, dateString)
dateString = "06 July 2008 7:32:47 AM"
ConvertToDateTime(outputBlock, dateString)
dateString = "17:32:47.003"
ConvertToDateTime(outputBlock, dateString)
End Sub
Private Sub ConvertToDateTime(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal value As Object)
Dim convertedDate As Date
Try
convertedDate = Convert.ToDateTime(value)
outputBlock.Text += String.Format("'{0}' converts to {1}.", value, convertedDate) & vbCrLf
Catch e As FormatException
outputBlock.Text += String.Format("'{0}' is not in the proper format.", value) & vbCrLf
Catch e As InvalidCastException
outputBlock.Text += String.Format("Conversion of the {0} '{1}' is not supported", _
value.GetType().Name, value) & vbCrLf
End Try
End Sub
End Module
' The example displays the following output:
' Conversion of the Int32 '16352' is not supported
' '' converts to 1/1/0001 12:00:00 AM.
' 'monthly' is not in the proper format.
' '05/01/1996' converts to 5/1/1996 12:00:00 AM.
' 'Tue Apr 28, 2009' converts to 4/28/2009 12:00:00 AM.
' '06 July 2008 7:32:47 AM' converts to 7/6/2008 7:32:47 AM.
' '17:32:47.003' converts to 5/28/2008 5:32:47 PM.
using System;
public class Example
{
private static System.Windows.Controls.TextBlock outputBlock;
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
Example.outputBlock = outputBlock;
// Try converting an integer.
int number = 16352;
ConvertToDateTime(number);
// Convert a null.
object obj = null;
ConvertToDateTime(obj);
// Convert a non-date string.
string nonDateString = "monthly";
ConvertToDateTime(nonDateString);
// Try to convert various date strings.
string dateString;
dateString = "05/01/1996";
ConvertToDateTime(dateString);
dateString = "Tue Apr 28, 2009";
ConvertToDateTime(dateString);
dateString = "06 July 2008 7:32:47 AM";
ConvertToDateTime(dateString);
dateString = "17:32:47.003";
ConvertToDateTime(dateString);
}
private static void ConvertToDateTime(object value)
{
DateTime convertedDate;
try
{
convertedDate = Convert.ToDateTime(value);
outputBlock.Text += String.Format("'{0}' converts to {1}.", value, convertedDate) + "\n";
}
catch (FormatException)
{
outputBlock.Text += String.Format("'{0}' is not in the proper format.", value) + "\n";
}
catch (InvalidCastException)
{
outputBlock.Text += String.Format("Conversion of the {0} '{1}' is not supported",
value.GetType().Name, value) + "\n";
}
}
}
// The example displays the following output:
// Conversion of the Int32 '16352' is not supported
// '' converts to 1/1/0001 12:00:00 AM.
// 'monthly' is not in the proper format.
// '05/01/1996' converts to 5/1/1996 12:00:00 AM.
// 'Tue Apr 28, 2009' converts to 4/28/2009 12:00:00 AM.
// '06 July 2008 7:32:47 AM' converts to 7/6/2008 7:32:47 AM.
// '17:32:47.003' converts to 5/28/2008 5:32:47 PM.
Version Information
Silverlight
Supported in: 5, 4, 3
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0
XNA Framework
Supported in: Xbox 360, Windows Phone OS 7.0
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.