Export (0) Print
Expand All

Convert.ToDateTime Method (Object, IFormatProvider)

Converts the value of the specified object to a DateTime object, using the specified culture-specific formatting information.

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

'Declaration
Public Shared Function ToDateTime ( _
	value As Object, _
	provider As IFormatProvider _
) As DateTime

Parameters

value
Type: System.Object

An object that implements the IConvertible interface.

provider
Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

Type: System.DateTime
The date and time equivalent of the value of value, or the date and time equivalent of DateTime.MinValue if value is Nothing.

ExceptionCondition
FormatException

value is not a valid date and time value.

InvalidCastException

value does not implement the IConvertible interface.

-or-

The conversion is not supported.

The return value is the result of invoking the IConvertible.ToDateTime method of the underlying type of value.

provider enables the user to specify culture-specific conversion information about the contents of value. For example, if value is a String that represents a date, provider could supply culture-specific information about the notation used to represent that date. provider is involved in the conversion of value if the runtime type of value is a String, or if value is a user-defined type whose IConvertible.ToDateTime implementation makes use of provider. If the runtime type of value is String and provider is Nothing, the CultureInfo object that represents the current thread culture is used.

The following example defines a custom format provider, CustomProvider, whose GetFormat method outputs a message to the console that it has been invoked, and then returns the DateTimeFormatInfo object of the culture whose name was passed as a parameter to its class constructor. Each of these CustomProvider objects is used to convert the elements in an object array to date and time values. The output indicates that the CustomProvider object is used in the conversion only when the type of the value parameter is a String.

Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "hu-HU", "pt-PT" }
      Dim objects() As Object = { 12, 17.2, False, #1/1/2010#, "today", _
                                  New System.Collections.ArrayList(), "c"c, _
                                  "05/10/2009 6:13:18 PM", "September 8, 1899" }

      For Each cultureName As String In cultureNames
         Console.WriteLine("{0} culture:", cultureName)
         Dim provider As New CustomProvider(cultureName)
         For Each obj As Object In objects
            Try 
               Dim dateValue As Date = Convert.ToDateTime(obj, provider)      
               Console.WriteLine("{0} --> {1}", obj, _
                                 dateValue.ToString(New CultureInfo(cultureName)))
            Catch e As FormatException
               Console.WriteLine("{0} --> Bad Format", obj)
            Catch e As InvalidCastException
               Console.WriteLine("{0} --> Conversion Not Supported", obj)
            End Try 
         Next
         Console.WriteLine()
      Next 
   End Sub 
End Module 

Public Class CustomProvider : Implements IFormatProvider
   Private cultureName As String 

   Public Sub New(cultureName As String)
      Me.cultureName = cultureName
   End Sub 

   Public Function GetFormat(formatType As Type) As Object _
          Implements IFormatProvider.GetFormat
      If formatType Is GetType(DateTimeFormatInfo) Then
         Console.Write("(CustomProvider retrieved.) ")
         Return New CultureInfo(cultureName).GetFormat(formatType)
      Else 
         Return Nothing 
      End If    
   End Function 
End Class 
' The example displays the following output: 
'    en-US culture: 
'    12 --> Conversion Not Supported 
'    17.2 --> Conversion Not Supported 
'    False --> Conversion Not Supported 
'    1/1/2010 12:00:00 AM --> 1/1/2010 12:00:00 AM 
'    (CustomProvider retrieved.) today --> Bad Format 
'    System.Collections.ArrayList --> Conversion Not Supported 
'    c --> Conversion Not Supported 
'    (CustomProvider retrieved.) 05/10/2009 6:13:18 PM --> 5/10/2009 6:13:18 PM 
'    (CustomProvider retrieved.) September 8, 1899 --> 9/8/1899 12:00:00 AM 
'     
'    hu-HU culture: 
'    12 --> Conversion Not Supported 
'    17.2 --> Conversion Not Supported 
'    False --> Conversion Not Supported 
'    1/1/2010 12:00:00 AM --> 2010. 01. 01. 0:00:00 
'    (CustomProvider retrieved.) today --> Bad Format 
'    System.Collections.ArrayList --> Conversion Not Supported 
'    c --> Conversion Not Supported 
'    (CustomProvider retrieved.) 05/10/2009 6:13:18 PM --> 2009. 05. 10. 18:13:18 
'    (CustomProvider retrieved.) September 8, 1899 --> 1899. 09. 08. 0:00:00 
'     
'    pt-PT culture: 
'    12 --> Conversion Not Supported 
'    17.2 --> Conversion Not Supported 
'    False --> Conversion Not Supported 
'    1/1/2010 12:00:00 AM --> 01-01-2010 0:00:00 
'    (CustomProvider retrieved.) today --> Bad Format 
'    System.Collections.ArrayList --> Conversion Not Supported 
'    c --> Conversion Not Supported 
'    (CustomProvider retrieved.) 05/10/2009 6:13:18 PM --> 05-10-2009 18:13:18 
'    (CustomProvider retrieved.) September 8, 1899 --> 08-09-1899 0:00:00

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft