Convert.ToDateTime Method (Object)


Converts the value of the specified object to a DateTime object.

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

Public Shared Function ToDateTime (
	value As Object
) As Date


Type: System.Object

An object that implements the IConvertible interface, or null.

Return Value

Type: System.DateTime

The date and time equivalent of the value of value, or a date and time equivalent of DateTime.MinValue if value is null.

Exception Condition

value is not a valid date and time value.


value does not implement the IConvertible interface.


The conversion is not supported.

For the conversion to succeed, the runtime type of the value parameter must be either a DateTime or a String, or value must be null. 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.

The following example calls the ToDateTime(Object) method with a variety of Object variables.

Module ConversionToDateTime
   Public Sub Main()
      ' Try converting an integer.
      Dim number As Integer = 16352

      ' Convert a null.
      Dim obj As Object = Nothing

      ' Convert a non-date string.
      Dim nonDateString As String = "monthly"

      ' Try to convert various dates.
      Dim dateString As String 
      dateString = "05/01/1996"
      dateString = "Tue Apr 28, 2009"
      dateString = "06 July 2008 7:32:47 AM"
      dateString = "17:32:47.003"
   End Sub

   Private Sub ConvertToDateTime(value As Object)
      Dim convertedDate As Date
         convertedDate = Convert.ToDateTime(value)
         Console.WriteLine("'{0}' converts to {1}.", value, convertedDate)
      Catch e As FormatException
         Console.WriteLine("'{0}' is not in the proper format.", value)
      Catch e As InvalidCastException
         Console.WriteLine("Conversion of the {0} '{1}' is not supported", _
                           value.GetType().Name, value)
      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.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top