Convert.ToDateTime Method

Definition

Converts a specified value to a DateTime value.

Overloads

ToDateTime(Single)

Calling this method always throws InvalidCastException.

ToDateTime(String)

Converts the specified string representation of a date and time to an equivalent date and time value.

ToDateTime(UInt16)

Calling this method always throws InvalidCastException.

ToDateTime(String, IFormatProvider)

Converts the specified string representation of a number to an equivalent date and time, using the specified culture-specific formatting information.

ToDateTime(UInt64)

Calling this method always throws InvalidCastException.

ToDateTime(Object, IFormatProvider)

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

ToDateTime(SByte)

Calling this method always throws InvalidCastException.

ToDateTime(UInt32)

Calling this method always throws InvalidCastException.

ToDateTime(Object)

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

ToDateTime(Int16)

Calling this method always throws InvalidCastException.

ToDateTime(Int32)

Calling this method always throws InvalidCastException.

ToDateTime(Int64)

Calling this method always throws InvalidCastException.

ToDateTime(Double)

Calling this method always throws InvalidCastException.

ToDateTime(Decimal)

Calling this method always throws InvalidCastException.

ToDateTime(DateTime)

Returns the specified DateTime object; no actual conversion is performed.

ToDateTime(Char)

Calling this method always throws InvalidCastException.

ToDateTime(Byte)

Calling this method always throws InvalidCastException.

ToDateTime(Boolean)

Calling this method always throws InvalidCastException.

ToDateTime(Single)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Calling this method always throws InvalidCastException.

C#
public static DateTime ToDateTime(float value);

Parameters

value
Single

The single-precision floating-point value to convert.

Returns

This conversion is not supported. No value is returned.

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(String)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Converts the specified string representation of a date and time to an equivalent date and time value.

C#
public static DateTime ToDateTime(string value);
C#
public static DateTime ToDateTime(string? value);

Parameters

value
String

The string representation of a date and time.

Returns

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

Exceptions

value is not a properly formatted date and time string.

Examples

The following example uses the ToDateTime method to convert various string representations of dates and times to DateTime values.

C#
using System;

public class ConversionToDateTime
{
   public static void Main()
   {
      string dateString = null;

      // Convert a null string.
      ConvertToDateTime(dateString);

      // Convert an empty string.
      dateString = String.Empty;
      ConvertToDateTime(dateString);

      // Convert a non-date string.
      dateString = "not a date";
      ConvertToDateTime(dateString);

      // Try to convert various date strings.
      dateString = "05/01/1996";
      ConvertToDateTime(dateString);
      dateString = "Tue Apr 28, 2009";
      ConvertToDateTime(dateString);
      dateString = "Wed Apr 28, 2009";
      ConvertToDateTime(dateString);
      dateString = "06 July 2008 7:32:47 AM";
      ConvertToDateTime(dateString);
      dateString = "17:32:47.003";
      ConvertToDateTime(dateString);
      // Convert a string returned by DateTime.ToString("R").
      dateString = "Sat, 10 May 2008 14:32:17 GMT";
      ConvertToDateTime(dateString);
      // Convert a string returned by DateTime.ToString("o").
      dateString = "2009-05-01T07:54:59.9843750-04:00";
      ConvertToDateTime(dateString);
   }

   private static void ConvertToDateTime(string value)
   {
      DateTime convertedDate;
      try {
         convertedDate = Convert.ToDateTime(value);
         Console.WriteLine("'{0}' converts to {1} {2} time.",
                           value, convertedDate,
                           convertedDate.Kind.ToString());
      }
      catch (FormatException) {
         Console.WriteLine("'{0}' is not in the proper format.", value);
      }
   }
}
// The example displays the following output:
//    '' converts to 1/1/0001 12:00:00 AM Unspecified time.
//    '' is not in the proper format.
//    'not a date' is not in the proper format.
//    '05/01/1996' converts to 5/1/1996 12:00:00 AM Unspecified time.
//    'Tue Apr 28, 2009' converts to 4/28/2009 12:00:00 AM Unspecified time.
//    'Wed Apr 28, 2009' is not in the proper format.
//    '06 July 2008 7:32:47 AM' converts to 7/6/2008 7:32:47 AM Unspecified time.
//    '17:32:47.003' converts to 5/30/2008 5:32:47 PM Unspecified time.
//    'Sat, 10 May 2008 14:32:17 GMT' converts to 5/10/2008 7:32:17 AM Local time.
//    '2009-05-01T07:54:59.9843750-04:00' converts to 5/1/2009 4:54:59 AM Local time.

Remarks

If value is not null, the return value is the result of invoking the DateTime.Parse method on value using the formatting information in a DateTimeFormatInfo object that is initialized for the current culture. The value argument must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic. If value is null, the method returns DateTime.MinValue.

This method tries to parse value completely and avoid throwing a FormatException. It completes missing month, day, and year information with the current date. If value contains only a date and no time, this method assumes a time of midnight. Any leading, inner, or trailing white-space characters in value are ignored.

If you prefer not to handle an exception if the conversion fails, you can call the DateTime.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToDateTime(UInt16)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Important

This API is not CLS-compliant.

Calling this method always throws InvalidCastException.

C#
[System.CLSCompliant(false)]
public static DateTime ToDateTime(ushort value);

Parameters

value
UInt16

The 16-bit unsigned integer to convert.

Returns

This conversion is not supported. No value is returned.

Attributes

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(String, IFormatProvider)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Converts the specified string representation of a number to an equivalent date and time, using the specified culture-specific formatting information.

C#
public static DateTime ToDateTime(string value, IFormatProvider provider);
C#
public static DateTime ToDateTime(string? value, IFormatProvider? provider);

Parameters

value
String

A string that contains a date and time to convert.

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

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

Exceptions

value is not a properly formatted date and time string.

Examples

The following example converts string representations of date values with the ToDateTime method, using an IFormatProvider object.

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      Console.WriteLine("{0,-18}{1,-12}{2}\n", "Date String", "Culture", "Result");

      string[] cultureNames = { "en-US", "ru-RU","ja-JP" };
      string[] dateStrings = { "01/02/09", "2009/02/03",  "01/2009/03",
                               "01/02/2009", "21/02/09", "01/22/09",
                               "01/02/23" };
      // Iterate each culture name in the array.
      foreach (string cultureName in cultureNames)
      {
         CultureInfo culture = new CultureInfo(cultureName);

         // Parse each date using the designated culture.
         foreach (string dateStr in dateStrings)
         {
            DateTime dateTimeValue;
            try {
               dateTimeValue = Convert.ToDateTime(dateStr, culture);
                // Display the date and time in a fixed format.
                Console.WriteLine("{0,-18}{1,-12}{2:yyyy-MMM-dd}",
                                  dateStr, cultureName, dateTimeValue);
            }
            catch (FormatException e) {
                Console.WriteLine("{0,-18}{1,-12}{2}",
                                  dateStr, cultureName, e.GetType().Name);
            }
         }
         Console.WriteLine();
      }
   }
}

Remarks

The return value is the result of invoking the DateTime.Parse(String, IFormatProvider) method on value.

provider is an IFormatProvider instance that obtains a DateTimeFormatInfo object. The DateTimeFormatInfo object provides culture-specific information about the format of value. If provider is null, the DateTimeFormatInfo for the current culture is used.

If you prefer not to handle an exception if the conversion fails, you can call the DateTime.TryParse method instead. It returns a Boolean value that indicates whether the conversion succeeded or failed.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToDateTime(UInt64)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Important

This API is not CLS-compliant.

Calling this method always throws InvalidCastException.

C#
[System.CLSCompliant(false)]
public static DateTime ToDateTime(ulong value);

Parameters

value
UInt64

The 64-bit unsigned integer to convert.

Returns

This conversion is not supported. No value is returned.

Attributes

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(Object, IFormatProvider)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

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

C#
public static DateTime ToDateTime(object value, IFormatProvider provider);
C#
public static DateTime ToDateTime(object? value, IFormatProvider? provider);

Parameters

value
Object

An object that implements the IConvertible interface.

provider
IFormatProvider

An object that supplies culture-specific formatting information.

Returns

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

Exceptions

value is not a valid date and time value.

value does not implement the IConvertible interface.

-or-

The conversion is not supported.

Examples

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.

C#
using System;
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(2010, 1, 1), "today",
                           new System.Collections.ArrayList(), 'c',
                           "05/10/2009 6:13:18 PM", "September 8, 1899" };

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

Remarks

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 null, the CultureInfo object that represents the current culture is used.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToDateTime(SByte)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Important

This API is not CLS-compliant.

Calling this method always throws InvalidCastException.

C#
[System.CLSCompliant(false)]
public static DateTime ToDateTime(sbyte value);

Parameters

value
SByte

The 8-bit signed integer to convert.

Returns

This conversion is not supported. No value is returned.

Attributes

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(UInt32)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Important

This API is not CLS-compliant.

Calling this method always throws InvalidCastException.

C#
[System.CLSCompliant(false)]
public static DateTime ToDateTime(uint value);

Parameters

value
UInt32

The 32-bit unsigned integer to convert.

Returns

This conversion is not supported. No value is returned.

Attributes

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(Object)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

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

C#
public static DateTime ToDateTime(object value);
C#
public static DateTime ToDateTime(object? value);

Parameters

value
Object

An object that implements the IConvertible interface, or null.

Returns

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

Exceptions

value is not a valid date and time value.

value does not implement the IConvertible interface.

-or-

The conversion is not supported.

Examples

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

C#
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.

Remarks

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.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

ToDateTime(Int16)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Calling this method always throws InvalidCastException.

C#
public static DateTime ToDateTime(short value);

Parameters

value
Int16

The 16-bit signed integer to convert.

Returns

This conversion is not supported. No value is returned.

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(Int32)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Calling this method always throws InvalidCastException.

C#
public static DateTime ToDateTime(int value);

Parameters

value
Int32

The 32-bit signed integer to convert.

Returns

This conversion is not supported. No value is returned.

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(Int64)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Calling this method always throws InvalidCastException.

C#
public static DateTime ToDateTime(long value);

Parameters

value
Int64

The 64-bit signed integer to convert.

Returns

This conversion is not supported. No value is returned.

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(Double)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Calling this method always throws InvalidCastException.

C#
public static DateTime ToDateTime(double value);

Parameters

value
Double

The double-precision floating-point value to convert.

Returns

This conversion is not supported. No value is returned.

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(Decimal)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Calling this method always throws InvalidCastException.

C#
public static DateTime ToDateTime(decimal value);

Parameters

value
Decimal

The number to convert.

Returns

This conversion is not supported. No value is returned.

Exceptions

This conversion is not supported.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(DateTime)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Returns the specified DateTime object; no actual conversion is performed.

C#
public static DateTime ToDateTime(DateTime value);

Parameters

value
DateTime

A date and time value.

Returns

value is returned unchanged.

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(Char)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Calling this method always throws InvalidCastException.

C#
public static DateTime ToDateTime(char value);

Parameters

value
Char

The Unicode character to convert.

Returns

This conversion is not supported. No value is returned.

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(Byte)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Calling this method always throws InvalidCastException.

C#
public static DateTime ToDateTime(byte value);

Parameters

value
Byte

The 8-bit unsigned integer to convert.

Returns

This conversion is not supported. No value is returned.

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1

ToDateTime(Boolean)

Source:
Convert.cs
Source:
Convert.cs
Source:
Convert.cs

Calling this method always throws InvalidCastException.

C#
public static DateTime ToDateTime(bool value);

Parameters

value
Boolean

The Boolean value to convert.

Returns

This conversion is not supported. No value is returned.

Exceptions

This conversion is not supported.

See also

Applies to

.NET 9 and other versions
Product Versions
.NET Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1