Convert.ToDateTime Method (Object, IFormatProvider) converts object to DateTime using culture-specific format : Convert « Development « VB.Net






Convert.ToDateTime Method (Object, IFormatProvider) converts object to DateTime using culture-specific format

  


Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "hu-HU", "pt-PT" }
      Dim objects() As Object = { #1/1/2010#, "today","05/10/2010 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("FormatException")
            Catch e As InvalidCastException
               Console.WriteLine("InvalidCastException")
            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

   
    
  








Related examples in the same category

1.Convert.ChangeType
2.Convert numeric string to SByte without a format provider
3.Convert numeric string to SByte with a format provider.
4.Converting strings to numbers in base
5.Convert.ToBase64CharArray converts 8-bit unsigned integer array to Unicode character array
6.Convert the Char array back to a Byte array
7.Convert.ToBase64String converts 8-bit unsigned integers to string encoded with base-64 digits.
8.Convert string by different base
9.Convert.ToDateTime (Object) converts object to a DateTime object.
10.Convert.ToDateTime(String) converts string to date and time value.
11.Convert.ToDateTime(String, IFormatProvider) converts string to date and time, using culture-specific format
12.Convert single value to decimal
13.Convert hex string to Int16
14.Convert hex string to int64
15.Convert Long to Integer
16.Convert double to Integer
17.Convert BigInteger to Integer
18.Convert decimal to Integer