閱讀英文

共用方式為


Convert.ToDateTime 方法

定義

將指定的值轉換為 DateTime 值。

多載

ToDateTime(Single)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(String)

將指定之日期和時間的字串表示,轉換為相等的日期和時間值。

ToDateTime(UInt16)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(String, IFormatProvider)

使用指定之特定文化特性格式資訊,將指定之數字的字串表示轉換為相等的日期和時間。

ToDateTime(UInt64)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(Object, IFormatProvider)

使用指定之特定文化特性格式資訊,將指定之物件的值轉換為 DateTime 物件。

ToDateTime(SByte)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(UInt32)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(Object)

將指定之物件的值轉換為 DateTime 物件。

ToDateTime(Int16)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(Int32)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(Int64)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(Double)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(Decimal)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(DateTime)

傳回指定的 DateTime 物件;不會執行實際的轉換。

ToDateTime(Char)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(Byte)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(Boolean)

呼叫這個方法一律會擲回 InvalidCastException

ToDateTime(Single)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

呼叫這個方法一律會擲回 InvalidCastException

public static DateTime ToDateTime (float value);

參數

value
Single

要轉換的單精確度浮點數值。

傳回

不支援此轉換。 沒有傳回值。

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

將指定之日期和時間的字串表示,轉換為相等的日期和時間值。

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

參數

value
String

日期和時間的字串表示。

傳回

與 值相等的value日期和時間,如果 valuenull,則為 DateTime.MinValue 的日期和時間對等專案。

例外狀況

value 不是格式正確的日期和時間字串。

範例

下列範例會 ToDateTime 使用 方法,將日期和時間的各種字串表示轉換為 DateTime 值。

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.

備註

如果 value 不是 null,則傳回值是使用對象中DateTimeFormatInfo針對目前文化特性初始化的格式資訊叫用 方法value的結果DateTime.Parse。 自 value 變數必須包含日期和時間的表示法,其格式為主題中所述 DateTimeFormatInfo 的其中一種格式。 如果 valuenull,則方法會傳回 DateTime.MinValue

這個方法會嘗試完全剖 value 析,並避免擲回 FormatException。 它會以目前日期完成遺漏的月份、日和年份資訊。 如果 value 只包含日期且沒有時間,則這個方法會假設午夜的時間。 中 value 任何前置、內部或尾端空格符都會被忽略。

如果您不想在轉換失敗時處理例外狀況,您可以改為呼叫 DateTime.TryParse 方法。 它會傳回值 Boolean ,指出轉換成功或失敗。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

重要

此 API 不符合 CLS 規範。

呼叫這個方法一律會擲回 InvalidCastException

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

參數

value
UInt16

要轉換之 16 位元不帶正負號的整數。

傳回

不支援此轉換。 沒有傳回值。

屬性

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

使用指定之特定文化特性格式資訊,將指定之數字的字串表示轉換為相等的日期和時間。

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

參數

value
String

字串,包含要轉換的日期和時間。

provider
IFormatProvider

物件,提供特定文化特性格式資訊。

傳回

與 值相等的value日期和時間,如果 valuenull,則為 DateTime.MinValue 的日期和時間對等專案。

例外狀況

value 不是格式正確的日期和時間字串。

範例

下列範例會使用 物件,以 ToDateTimeIFormatProvider 方法轉換日期值的字串表示。

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();
      }
   }
}

備註

傳回值是在 上value叫用 DateTime.Parse(String, IFormatProvider) 方法的結果。

provider IFormatProvider是取得 DateTimeFormatInfo 對象的實例。 物件 DateTimeFormatInfo 提供有關 格式 value的文化特性特定資訊。 如果 為 providernullDateTimeFormatInfo 則會使用目前文化特性的 。

如果您不想在轉換失敗時處理例外狀況,您可以改為呼叫 DateTime.TryParse 方法。 它會傳回值 Boolean ,指出轉換成功或失敗。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

重要

此 API 不符合 CLS 規範。

呼叫這個方法一律會擲回 InvalidCastException

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

參數

value
UInt64

要轉換之 64 位元不帶正負號的整數。

傳回

不支援此轉換。 沒有傳回值。

屬性

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

使用指定之特定文化特性格式資訊,將指定之物件的值轉換為 DateTime 物件。

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

參數

value
Object

實作 IConvertible 介面的物件。

provider
IFormatProvider

物件,提供特定文化特性格式資訊。

傳回

與 值相等的value日期和時間,如果 valuenull,則為 DateTime.MinValue 的日期和時間對等專案。

例外狀況

value 不是有效的日期和時間值。

value 未實作 IConvertible 介面。

-或-

不支援轉換。

範例

下列範例會定義自定義格式提供者 CustomProvider,其 GetFormat 方法會將訊息輸出至已叫用的控制台,然後傳回 DateTimeFormatInfo 文化特性的物件,其名稱會當做參數傳遞至其類別建構函式。 每 CustomProvider 一個物件都會用來將物件數位中的項目轉換成日期和時間值。 輸出表示 CustomProvider 只有在參數的類型 valueString時,才會在轉換中使用物件。

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

備註

傳回值是叫用 IConvertible.ToDateTime 基礎類型 value之 方法的結果。

provider 可讓使用者指定 有關 內容 value的文化特性特定轉換資訊。 例如,如果 valueString 代表日期的 , provider 可能會提供用來表示該日期之表示法的文化特性特定資訊。 provider如果的運行時間型value別是 String,或 value 是實IConvertible.ToDateTime作會使用provider的使用者定義型別,則與的轉換value有關。 如果的 value 運行時間類型為 String ,且 providernull,則會 CultureInfo 使用代表目前文化特性的物件。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

重要

此 API 不符合 CLS 規範。

呼叫這個方法一律會擲回 InvalidCastException

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

參數

value
SByte

要轉換的 8 位元帶正負號的整數。

傳回

不支援此轉換。 沒有傳回值。

屬性

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

重要

此 API 不符合 CLS 規範。

呼叫這個方法一律會擲回 InvalidCastException

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

參數

value
UInt32

要轉換之 32 位元不帶正負號的整數。

傳回

不支援此轉換。 沒有傳回值。

屬性

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

將指定之物件的值轉換為 DateTime 物件。

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

參數

value
Object

實作 IConvertible 介面的物件,或是 null

傳回

與 值相等的value日期和時間,如果 valuenull,則為 DateTime.MinValue 的日期和時間對等專案。

例外狀況

value 不是有效的日期和時間值。

value 未實作 IConvertible 介面。

-或-

不支援轉換。

範例

下列範例會使用各種Object變數呼叫 ToDateTime(Object) 方法。

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.

備註

若要讓轉換成功,參數的 value 執行時間類型必須是 DateTimeString,或 value 必須是 null。 否則,方法會擲回 InvalidCastException。 此外,如果 value 是字串,它必須包含目前文化特性中日期和時間值的有效表示法,否則 FormatException 會擲回 。

傳回值是叫 IConvertible.ToDateTime 用 基礎型 value別 之 方法的結果。

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

呼叫這個方法一律會擲回 InvalidCastException

public static DateTime ToDateTime (short value);

參數

value
Int16

要轉換的 16 位元帶正負號的整數。

傳回

不支援此轉換。 沒有傳回值。

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

呼叫這個方法一律會擲回 InvalidCastException

public static DateTime ToDateTime (int value);

參數

value
Int32

要轉換的 32 位元帶正負號的整數。

傳回

不支援此轉換。 沒有傳回值。

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

呼叫這個方法一律會擲回 InvalidCastException

public static DateTime ToDateTime (long value);

參數

value
Int64

要轉換的 64 位元帶正負號的整數。

傳回

不支援此轉換。 沒有傳回值。

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

呼叫這個方法一律會擲回 InvalidCastException

public static DateTime ToDateTime (double value);

參數

value
Double

要轉換的雙精確度浮點數值。

傳回

不支援此轉換。 沒有傳回值。

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

呼叫這個方法一律會擲回 InvalidCastException

public static DateTime ToDateTime (decimal value);

參數

value
Decimal

要轉換的數字。

傳回

不支援此轉換。 沒有傳回值。

例外狀況

不支援此轉換。

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

傳回指定的 DateTime 物件;不會執行實際的轉換。

public static DateTime ToDateTime (DateTime value);

參數

value
DateTime

日期和時間值。

傳回

value 會原封不動地傳回。

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

呼叫這個方法一律會擲回 InvalidCastException

public static DateTime ToDateTime (char value);

參數

value
Char

要轉換的 Unicode 字元。

傳回

不支援此轉換。 沒有傳回值。

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

呼叫這個方法一律會擲回 InvalidCastException

public static DateTime ToDateTime (byte value);

參數

value
Byte

要轉換之 8 位元不帶正負號的整數。

傳回

不支援此轉換。 沒有傳回值。

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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)

來源:
Convert.cs
來源:
Convert.cs
來源:
Convert.cs

呼叫這個方法一律會擲回 InvalidCastException

public static DateTime ToDateTime (bool value);

參數

value
Boolean

要轉換的布林值。

傳回

不支援此轉換。 沒有傳回值。

例外狀況

不支援此轉換。

另請參閱

適用於

.NET 9 及其他版本
產品 版本
.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