This topic has not yet been rated - Rate this topic

Convert.ToDateTime Method (Object)

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

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
public static DateTime ToDateTime(
	Object value
)

Parameters

value
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.
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.

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.

using System;

public class ConversionToDateTime
{
   public static void Main()
   {
      // 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);
         Console.WriteLine("'{0}' converts to {1}.", value, convertedDate);
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in the proper format.", value);
      }   
      catch (InvalidCastException) {
         Console.WriteLine("Conversion of the {0} '{1}' is not supported", 
                           value.GetType().Name, value);
      }
   }
}
// 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.

.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

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.

Did you find this helpful?
(1500 characters remaining)
© 2013 Microsoft. All rights reserved.