C# Convert ToDateTime(Object, IFormatProvider)

Description

Convert ToDateTime(Object, IFormatProvider) converts the value of the specified object to a DateTime object, using the specified culture-specific formatting information.

Syntax

Convert.ToDateTime(Object, IFormatProvider) has the following syntax.


public static DateTime ToDateTime(
  Object value,
  IFormatProvider provider
)

Parameters

Convert.ToDateTime(Object, IFormatProvider) has the following parameters.

  • value - An object that implements the IConvertible interface.
  • provider - An object that supplies culture-specific formatting information.

Returns

Convert.ToDateTime(Object, IFormatProvider) method returns The date and time equivalent of the value of value, or the date and time equivalent of DateTime.MinValue if value is null.

Example

The following code shows how to use Convert.ToDateTime(Object, IFormatProvider) method.


using System;//from w w  w  .j ava2  s  .  c  o m
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] cultureNames = { "en-US", "hu-HU", "pt-PT" };
      object[] objects = { 12, 17.2, false, new DateTime(2014, 1, 1), "today", 
                           new System.Collections.ArrayList(), 'c', 
                           "05/10/2014 6:13:18 PM", "September 8, 2014" };

      foreach (string cultureName in cultureNames)
      {
         Console.WriteLine("{0} culture:", cultureName);
         CustomProvider provider = new CustomProvider(cultureName);
         foreach (object obj in objects)
         {            
            try {
               DateTime dateValue = Convert.ToDateTime(obj, provider);      
               Console.WriteLine("{0} --> {1}", obj, 
                                 dateValue.ToString(new CultureInfo(cultureName)));
            }
            catch (FormatException) {
               Console.WriteLine("{0} --> Bad Format", obj);
            }   
            catch (InvalidCastException) {
               Console.WriteLine("{0} --> Conversion Not Supported", obj);
            }
         }
         Console.WriteLine();
      }
   }
}

public class CustomProvider : IFormatProvider
{
   private string cultureName;

   public CustomProvider(string cultureName)
   {
      this.cultureName = cultureName;
   }

   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(DateTimeFormatInfo))
      {
         Console.Write("(CustomProvider retrieved.) ");
         return new CultureInfo(cultureName).GetFormat(formatType);
      }
      else
      {
         return null;
      }   
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version