Convert.ToDateTime 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將指定的值轉換為某個 DateTime 值。
多載
| 名稱 | Description |
|---|---|
| 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(Double) |
呼叫此方法總是拋 InvalidCastException出 。 |
| ToDateTime(Int32) |
呼叫此方法總是拋 InvalidCastException出 。 |
| ToDateTime(Int16) |
呼叫此方法總是拋 InvalidCastException出 。 |
| ToDateTime(Int64) |
呼叫此方法總是拋 InvalidCastException出 。 |
| ToDateTime(Decimal) |
呼叫此方法總是拋 InvalidCastException出 。 |
| ToDateTime(DateTime) |
回傳指定的 DateTime 物件;不會實際進行轉換。 |
| ToDateTime(Char) |
呼叫此方法總是拋 InvalidCastException出 。 |
| ToDateTime(Byte) |
呼叫此方法總是拋 InvalidCastException出 。 |
| ToDateTime(Boolean) |
呼叫此方法總是拋 InvalidCastException出 。 |
ToDateTime(Single)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(float value);
public static DateTime ToDateTime(float value);
static member ToDateTime : single -> DateTime
Public Shared Function ToDateTime (value As Single) As DateTime
參數
- value
- Single
要轉換的單精度浮點數。
傳回
此轉換不被支援。 沒有任何價值回傳。
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(String)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
將指定的字串表示日期與時間轉換為等效的日期與時間值。
public:
static DateTime ToDateTime(System::String ^ value);
public static DateTime ToDateTime(string value);
public static DateTime ToDateTime(string? value);
static member ToDateTime : string -> DateTime
Public Shared Function ToDateTime (value As String) As DateTime
參數
- value
- String
日期與時間的串表示法。
傳回
若為 value,則為 的日期與時間等價值,或 DateTime.MinValue 的日期與時間等價value值。null
例外狀況
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.
open System
let convertToDateTime (value: string) =
try
let convertedDate = Convert.ToDateTime value
printfn $"'{value}' converts to {convertedDate} {convertedDate.Kind} time."
with :?FormatException ->
printfn $"'{value}' is not in the proper format."
[<EntryPoint>]
let main _ =
let dateString = null
// Convert a null string.
convertToDateTime dateString
// Convert an empty string.
let dateString = String.Empty
convertToDateTime dateString
// Convert a non-date string.
let dateString = "not a date"
convertToDateTime dateString
// Try to convert various date strings.
let dateString = "05/01/1996"
convertToDateTime dateString
let dateString = "Tue Apr 28, 2009"
convertToDateTime dateString
let dateString = "Wed Apr 28, 2009"
convertToDateTime dateString
let dateString = "06 July 2008 7:32:47 AM"
convertToDateTime dateString
let dateString = "17:32:47.003"
convertToDateTime dateString
// Convert a string returned by DateTime.ToString("R").
let dateString = "Sat, 10 May 2008 14:32:17 GMT"
convertToDateTime dateString
// Convert a string returned by DateTime.ToString("o").
let dateString = "2009-05-01T07:54:59.9843750-04:00"
convertToDateTime dateString
0
// 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.
Module ConversionToDateTime
Public Sub Main()
Dim dateString As String = Nothing
' 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)
End Sub
Private Sub ConvertToDateTime(value As String)
Dim convertedDate As Date
Try
convertedDate = Convert.ToDateTime(value)
Console.WriteLine("'{0}' converts to {1}.", value, convertedDate)
Catch e As FormatException
Console.WriteLine("'{0}' is not in the proper format.", value)
End Try
End Sub
End Module
' The example displays the following output:
' '' converts to 1/1/0001 12:00:00 AM.
' '' 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.
' 'Tue Apr 28, 2009' converts to 4/28/2009 12:00:00 AM.
' '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.
' '17:32:47.003' converts to 5/30/2008 5:32:47 PM.
' 'Sat, 10 May 2008 14:32:17 GMT' converts to 5/10/2008 7:32:17 AM.
' '2009-05-01T07:54:59.9843750-04:00' converts to 5/1/2009 4:54:59 AM.
備註
若 value 不是 null,則回傳值是使用 DateTime.Parse 該方法 value 在物件中初始化為當前文化的格式資訊 DateTimeFormatInfo 所產生的結果。 論 value 證必須包含以主題 DateTimeFormatInfo 中描述的格式之一表示日期與時間。 若 value , null則方法返回 DateTime.MinValue。
此方法嘗試完整解析 value 並避免拋 FormatException出 。 它會補足缺失的月份、日期和年份資訊,並附上當前日期。 若 value 只包含日期且無時間,此方法假設時間為午夜。 任何前置、內側或後方的空白字 value 元都被忽略。
如果你不想在轉換失敗時處理例外,可以直接呼叫該 DateTime.TryParse 方法。 它會回傳 Boolean 一個值,表示轉換是否成功。
另請參閱
- CurrentCulture
解析 .NET
適用於
ToDateTime(UInt16)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
重要
此 API 不符合 CLS 規範。
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(System::UInt16 value);
[System.CLSCompliant(false)]
public static DateTime ToDateTime(ushort value);
[<System.CLSCompliant(false)>]
static member ToDateTime : uint16 -> DateTime
Public Shared Function ToDateTime (value As UShort) As DateTime
參數
- value
- UInt16
16位元無符號整數要轉換。
傳回
此轉換不被支援。 沒有任何價值回傳。
- 屬性
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(String, IFormatProvider)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
將指定的數字字串表示轉換為相當的日期與時間,並使用指定的文化特定格式資訊。
public:
static DateTime ToDateTime(System::String ^ value, IFormatProvider ^ provider);
public static DateTime ToDateTime(string value, IFormatProvider provider);
public static DateTime ToDateTime(string? value, IFormatProvider? provider);
static member ToDateTime : string * IFormatProvider -> DateTime
Public Shared Function ToDateTime (value As String, provider As IFormatProvider) As DateTime
參數
- value
- String
字串,包含要轉換的日期和時間。
- provider
- IFormatProvider
物件,提供特定文化特性的格式資訊。
傳回
若為 value,則為 的日期與時間等價值,或 DateTime.MinValue 的日期與時間等價value值。null
例外狀況
value 不是正確格式化的日期和時間字串。
範例
以下範例使用物件將日期值的字串表示轉換 ToDateTime 成該方法 IFormatProvider 。
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();
}
}
}
open System
open System.Globalization
printfn $"""{"Date String",-18}{"Culture",-12}{"Result"}\n"""
let cultureNames = [ "en-US"; "ru-RU"; "ja-JP" ]
let 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.
for cultureName in cultureNames do
let culture = CultureInfo cultureName
// Parse each date using the designated culture.
for dateStr in dateStrings do
try
let dateTimeValue = Convert.ToDateTime(dateStr, culture)
// Display the date and time in a fixed format.
printfn $"""{dateStr,-18}{cultureName,-12}{dateTimeValue.ToString "yyyy-MMM-dd"}"""
with :? FormatException as e ->
printfn $"{dateStr,-18}{cultureName,-12}{e.GetType().Name}"
printfn ""
Imports System.Globalization
Module Example
Public Sub Main( )
Console.WriteLine("{0,-18}{1,-12}{2}", "Date String", "Culture", "Result")
Console.WriteLine()
Dim cultureNames() As String = { "en-US", "ru-RU","ja-JP" }
Dim dateStrings() As String = { "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.
For Each cultureName As String In cultureNames
Dim culture As CultureInfo = New CultureInfo(cultureName)
' Parse each date using the designated culture.
For Each dateStr As String In dateStrings
Dim dateTimeValue As DateTime
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 e As FormatException
Console.WriteLine("{0,-18}{1,-12}{2}", _
dateStr, cultureName, e.GetType().Name)
End Try
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Date String Culture Result
'
' 01/02/09 en-US 2009-Jan-02
' 2009/02/03 en-US 2009-Feb-03
' 01/2009/03 en-US 2009-Jan-03
' 01/02/2009 en-US 2009-Jan-02
' 21/02/09 en-US FormatException
' 01/22/09 en-US 2009-Jan-22
' 01/02/23 en-US 2023-Jan-02
'
' 01/02/09 ru-RU 2009-Feb-01
' 2009/02/03 ru-RU 2009-Feb-03
' 01/2009/03 ru-RU 2009-Jan-03
' 01/02/2009 ru-RU 2009-Feb-01
' 21/02/09 ru-RU 2009-Feb-21
' 01/22/09 ru-RU FormatException
' 01/02/23 ru-RU 2023-Feb-01
'
' 01/02/09 ja-JP 2001-Feb-09
' 2009/02/03 ja-JP 2009-Feb-03
' 01/2009/03 ja-JP 2009-Jan-03
' 01/02/2009 ja-JP 2009-Jan-02
' 21/02/09 ja-JP 2021-Feb-09
' 01/22/09 ja-JP FormatException
' 01/02/23 ja-JP 2001-Feb-23
備註
回傳值是呼叫該方法後DateTime.Parse(String, IFormatProvider)value的結果。
provider 是一個 IFormatProvider 獲得物件 DateTimeFormatInfo 的實例。 該 DateTimeFormatInfo 物件提供關於 格式 value的文化特定資訊。 若 provider ,nullDateTimeFormatInfo則 為當前文化。
如果你不想在轉換失敗時處理例外,可以直接呼叫該 DateTime.TryParse 方法。 它會回傳 Boolean 一個值,表示轉換是否成功。
另請參閱
解析 .NET
適用於
ToDateTime(UInt64)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
重要
此 API 不符合 CLS 規範。
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(System::UInt64 value);
[System.CLSCompliant(false)]
public static DateTime ToDateTime(ulong value);
[<System.CLSCompliant(false)>]
static member ToDateTime : uint64 -> DateTime
Public Shared Function ToDateTime (value As ULong) As DateTime
參數
- value
- UInt64
64 位元無符號整數用於轉換。
傳回
此轉換不被支援。 沒有任何價值回傳。
- 屬性
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(Object, IFormatProvider)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
利用指定的文化特有格式資訊,將指定物件的值轉換為物件 DateTime 。
public:
static DateTime ToDateTime(System::Object ^ value, IFormatProvider ^ provider);
public static DateTime ToDateTime(object value, IFormatProvider provider);
public static DateTime ToDateTime(object? value, IFormatProvider? provider);
static member ToDateTime : obj * IFormatProvider -> DateTime
Public Shared Function ToDateTime (value As Object, provider As IFormatProvider) As DateTime
參數
- value
- Object
一個實作介面的 IConvertible 物件。
- provider
- IFormatProvider
物件,提供特定文化特性的格式資訊。
傳回
若為 value,則為 的日期與時間等價值,或 DateTime.MinValue 的日期與時間等價value值。null
例外狀況
value 不是一個有效的日期和時間值。
範例
以下範例定義了一個自訂格式提供者,其CustomProviderGetFormat方法會向主控台輸出訊息,告知已被調用,然後回傳DateTimeFormatInfo該文化中名稱作為參數傳入類別建構器的物件。 這些 CustomProvider 物件用來將物件陣列中的元素轉換為日期與時間值。 輸出表示 CustomProvider 該物件僅在參數類型 value 為 String時才用於轉換。
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
open System
open System.Globalization
type CustomProvider(cultureName: string) =
interface IFormatProvider with
member _.GetFormat(formatType) =
if formatType = typeof<DateTimeFormatInfo> then
printf "(CustomProvider retrieved.) "
CultureInfo(cultureName).GetFormat formatType
else
null
let cultureNames = [ "en-US"; "hu-HU"; "pt-PT" ]
let objects: obj list =
[ 12; 17.2; false; DateTime(2010, 1, 1); "today"
System.Collections.ArrayList(); 'c'
"05/10/2009 6:13:18 PM"; "September 8, 1899" ]
for cultureName in cultureNames do
printfn $"{cultureName} culture:"
let provider = CustomProvider cultureName
for obj in objects do
try
let dateValue = Convert.ToDateTime(obj, provider)
printfn $"{obj} --> {dateValue.ToString(CultureInfo cultureName)}"
with
| :? FormatException ->
printfn $"{obj} --> Bad Format"
| :? InvalidCastException ->
printfn $"{obj} --> Conversion Not Supported"
printfn ""
// 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
Imports System.Globalization
Module Example
Public Sub Main()
Dim cultureNames() As String = { "en-US", "hu-HU", "pt-PT" }
Dim objects() As Object = { 12, 17.2, False, #1/1/2010#, "today", _
New System.Collections.ArrayList(), "c"c, _
"05/10/2009 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("{0} --> Bad Format", obj)
Catch e As InvalidCastException
Console.WriteLine("{0} --> Conversion Not Supported", obj)
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
' 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的文化專屬轉換資訊。 例如,若 value 代表日期的 a String , provider 則可提供該日期所用符號的文化特定資訊。
provider若執行時value型別為 value,或String為使用者定義型別且實value作使用 ,IConvertible.ToDateTime則 參與 的轉換provider。 若執行時型value態為Stringprovider且為,nullCultureInfo則使用代表當前文化的物件。
另請參閱
解析 .NET
適用於
ToDateTime(SByte)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
重要
此 API 不符合 CLS 規範。
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(System::SByte value);
[System.CLSCompliant(false)]
public static DateTime ToDateTime(sbyte value);
[<System.CLSCompliant(false)>]
static member ToDateTime : sbyte -> DateTime
Public Shared Function ToDateTime (value As SByte) As DateTime
參數
- value
- SByte
8位元有符號整數用於轉換。
傳回
此轉換不被支援。 沒有任何價值回傳。
- 屬性
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(UInt32)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
重要
此 API 不符合 CLS 規範。
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(System::UInt32 value);
[System.CLSCompliant(false)]
public static DateTime ToDateTime(uint value);
[<System.CLSCompliant(false)>]
static member ToDateTime : uint32 -> DateTime
Public Shared Function ToDateTime (value As UInteger) As DateTime
參數
- value
- UInt32
32位元無符號整數要轉換。
傳回
此轉換不被支援。 沒有任何價值回傳。
- 屬性
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(Object)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
將指定物件的值轉換成物件。DateTime
public:
static DateTime ToDateTime(System::Object ^ value);
public static DateTime ToDateTime(object value);
public static DateTime ToDateTime(object? value);
static member ToDateTime : obj -> DateTime
Public Shared Function ToDateTime (value As Object) As DateTime
參數
- value
- Object
一個實作介面的 IConvertible 物件,或 null。
傳回
若 為 value,則為 的日期與時間等價值,或 DateTime.MinValue 的日期與時間等價value值。null
例外狀況
value 不是一個有效的日期和時間值。
範例
以下範例以多種ToDateTime(Object)變數呼叫該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.
open System
let convertToDateTime (value: obj) =
try
let convertedDate = Convert.ToDateTime value
printfn $"'{value}' converts to {convertedDate}."
with
| :? FormatException ->
printfn $"'{value}' is not in the proper format."
| :? InvalidCastException ->
printfn $"Conversion of the {value.GetType().Name} '{value}' is not supported"
[<EntryPoint>]
let main _ =
// Try converting an integer.
let number = 16352
convertToDateTime number
// Convert a null.
let obj = box null
convertToDateTime obj
// Convert a non-date string.
let nonDateString = "monthly"
convertToDateTime nonDateString
// Try to convert various date strings.
let dateString = "05/01/1996"
convertToDateTime dateString
let dateString = "Tue Apr 28, 2009"
convertToDateTime dateString
let dateString = "06 July 2008 7:32:47 AM"
convertToDateTime dateString
let dateString = "17:32:47.003"
convertToDateTime dateString
0
// 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.
Module ConversionToDateTime
Public Sub Main()
' Try converting an integer.
Dim number As Integer = 16352
ConvertToDateTime(number)
' Convert a null.
Dim obj As Object = Nothing
ConvertToDateTime(obj)
' Convert a non-date string.
Dim nonDateString As String = "monthly"
ConvertToDateTime(nonDateString)
' Try to convert various dates.
Dim dateString As String
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)
End Sub
Private Sub ConvertToDateTime(value As Object)
Dim convertedDate As Date
Try
convertedDate = Convert.ToDateTime(value)
Console.WriteLine("'{0}' converts to {1}.", value, convertedDate)
Catch e As FormatException
Console.WriteLine("'{0}' is not in the proper format.", value)
Catch e As InvalidCastException
Console.WriteLine("Conversion of the {0} '{1}' is not supported", _
value.GetType().Name, value)
End Try
End Sub
End Module
' 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 執行時類型必須是 a DateTime 或 Stringa,或 value 是 null。 否則,該方法會拋出一個 InvalidCastException。 此外,若 value 為字串,必須包含當前文化中有效表示的日期與時間值,否則會拋出 a FormatException 。
回傳值是調 IConvertible.ToDateTime 用底層類型 value方法的結果。
適用於
ToDateTime(Double)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(double value);
public static DateTime ToDateTime(double value);
static member ToDateTime : double -> DateTime
Public Shared Function ToDateTime (value As Double) As DateTime
參數
- value
- Double
要轉換的雙精確度浮點數。
傳回
此轉換不被支援。 沒有任何價值回傳。
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(Int32)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(int value);
public static DateTime ToDateTime(int value);
static member ToDateTime : int -> DateTime
Public Shared Function ToDateTime (value As Integer) As DateTime
參數
- value
- Int32
要轉換的32位帶正負號整數。
傳回
此轉換不被支援。 沒有任何價值回傳。
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(Int16)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(short value);
public static DateTime ToDateTime(short value);
static member ToDateTime : int16 -> DateTime
Public Shared Function ToDateTime (value As Short) As DateTime
參數
- value
- Int16
要轉換的16位帶正負號整數。
傳回
此轉換不被支援。 沒有任何價值回傳。
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(Int64)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(long value);
public static DateTime ToDateTime(long value);
static member ToDateTime : int64 -> DateTime
Public Shared Function ToDateTime (value As Long) As DateTime
參數
- value
- Int64
要轉換的64位帶正負號整數。
傳回
此轉換不被支援。 沒有任何價值回傳。
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(Decimal)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(System::Decimal value);
public static DateTime ToDateTime(decimal value);
static member ToDateTime : decimal -> DateTime
Public Shared Function ToDateTime (value As Decimal) As DateTime
參數
- value
- Decimal
要轉換的數位。
傳回
此轉換不被支援。 沒有任何價值回傳。
例外狀況
此轉換不被支援。
適用於
ToDateTime(DateTime)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
回傳指定的 DateTime 物件;不會實際進行轉換。
public:
static DateTime ToDateTime(DateTime value);
public static DateTime ToDateTime(DateTime value);
static member ToDateTime : DateTime -> DateTime
Public Shared Function ToDateTime (value As DateTime) As DateTime
參數
- value
- DateTime
日期和時間值。
傳回
value 未改變地返回。
適用於
ToDateTime(Char)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(char value);
public static DateTime ToDateTime(char value);
static member ToDateTime : char -> DateTime
Public Shared Function ToDateTime (value As Char) As DateTime
參數
- value
- Char
要轉換的 Unicode 字元。
傳回
此轉換不被支援。 沒有任何價值回傳。
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(Byte)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(System::Byte value);
public static DateTime ToDateTime(byte value);
static member ToDateTime : byte -> DateTime
Public Shared Function ToDateTime (value As Byte) As DateTime
參數
- value
- Byte
要轉換的是 8 位元無符號整數。
傳回
此轉換不被支援。 沒有任何價值回傳。
例外狀況
此轉換不被支援。
另請參閱
適用於
ToDateTime(Boolean)
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
- 來源:
- Convert.cs
呼叫此方法總是拋 InvalidCastException出 。
public:
static DateTime ToDateTime(bool value);
public static DateTime ToDateTime(bool value);
static member ToDateTime : bool -> DateTime
Public Shared Function ToDateTime (value As Boolean) As DateTime
參數
- value
- Boolean
轉換時的布林值。
傳回
此轉換不被支援。 沒有任何價值回傳。
例外狀況
此轉換不被支援。