Convert.ToDateTime 方法
定义
重载
| ToDateTime(Single) |
调用此方法始终引发 InvalidCastException。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) |
调用此方法始终引发 InvalidCastException。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) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(Object, IFormatProvider) |
使用指定的区域性特定格式设置信息将指定对象的值转换为 DateTime 对象。Converts the value of the specified object to a DateTime object, using the specified culture-specific formatting information. |
| ToDateTime(SByte) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(UInt32) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(Object) |
将指定对象的值转换为 DateTime 对象。Converts the value of the specified object to a DateTime object. |
| ToDateTime(Double) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(Int32) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(Int16) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(Int64) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(Decimal) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(DateTime) |
返回指定的 DateTime 对象;不执行任何实际的转换。Returns the specified DateTime object; no actual conversion is performed. |
| ToDateTime(Char) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(Byte) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
| ToDateTime(Boolean) |
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException. |
ToDateTime(Single)
调用此方法始终引发 InvalidCastException。Calling this method always throws 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
要转换的单精度浮点值。The single-precision floating-point value to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(String)
将日期和时间的指定字符串表示形式转换为等效的日期和时间值。Converts the specified string representation of a date and time to an equivalent date and time value.
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
日期和时间的字符串表示形式。The string representation of a date and time.
返回
value 的值的日期和时间等效项,如果 MinValue 为 value,则为 null 的日期和时间等效项。The date and time equivalent of the value of value, or the date and time equivalent of MinValue if value is null.
例外
value 不是格式正确的日期和时间字符串。value is not a properly formatted date and time string.
示例
下面的示例使用 ToDateTime 方法将日期和时间的各种字符串表示形式转换为 DateTime 值。The following example uses the ToDateTime method to convert various string representations of dates and times to DateTime values.
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.
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 当前区域性初始化的对象中的格式设置信息。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. value参数必须以主题中所述的格式之一包含日期和时间的表示形式 DateTimeFormatInfo 。The value argument must contain the representation of a date and time in one of the formats described in the DateTimeFormatInfo topic. 如果 value 为 null,则此方法返回 DateTime.MinValue。If value is null, the method returns DateTime.MinValue.
此方法尝试完全分析 value 并且避免引发 FormatException 。This method tries to parse value completely and avoid throwing a FormatException. 它完成缺少包含当前日期的月、日和年信息。It completes missing month, day, and year information with the current date. 如果 value 仅包含日期而不包含时间,则此方法假定时间为午夜。If value contains only a date and no time, this method assumes a time of midnight. 中的所有前导、内部或尾随空白字符 value 都将被忽略。Any leading, inner, or trailing white-space characters in value are ignored.
如果你不希望在转换失败时处理异常,则可以改为调用 DateTime.TryParse 方法。If you prefer not to handle an exception if the conversion fails, you can call the DateTime.TryParse method instead. 它将返回一个 Boolean 值,该值指示转换是成功还是失败。It returns a Boolean value that indicates whether the conversion succeeded or failed.
另请参阅
适用于
ToDateTime(UInt16)
重要
此 API 不符合 CLS。
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException.
public:
static DateTime ToDateTime(System::UInt16 value);
[System.CLSCompliant(false)]
public static DateTime ToDateTime (ushort value);
public static DateTime ToDateTime (ushort value);
[<System.CLSCompliant(false)>]
static member ToDateTime : uint16 -> DateTime
static member ToDateTime : uint16 -> DateTime
Public Shared Function ToDateTime (value As UShort) As DateTime
参数
- value
- UInt16
要转换的 16 位无符号整数。The 16-bit unsigned integer to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
- 属性
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(String, IFormatProvider)
使用指定的区域性特定格式设置信息,将数字的指定字符串表示形式转换为等效的日期和时间。Converts the specified string representation of a number to an equivalent date and time, using the specified culture-specific formatting information.
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
包含要转换的日期和时间的字符串。A string that contains a date and time to convert.
- provider
- IFormatProvider
一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.
返回
value 的值的日期和时间等效项,如果 MinValue 为 value,则为 null 的日期和时间等效项。The date and time equivalent of the value of value, or the date and time equivalent of MinValue if value is null.
例外
value 不是格式正确的日期和时间字符串。value is not a properly formatted date and time string.
示例
下面的示例使用对象通过方法转换 date 值的字符串表示形式 ToDateTime IFormatProvider 。The following example converts string representations of date values with the ToDateTime method, using an IFormatProvider object.
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();
}
}
}
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 。The return value is the result of invoking the DateTime.Parse(String, IFormatProvider) method on value.
provider 是一个 IFormatProvider 获取对象的实例 DateTimeFormatInfo 。provider is an IFormatProvider instance that obtains a DateTimeFormatInfo object. DateTimeFormatInfo对象提供有关的格式的区域性特定信息 value 。The DateTimeFormatInfo object provides culture-specific information about the format of value. 如果 provider 为 null ,则 DateTimeFormatInfo 使用当前区域性的。If provider is null, the DateTimeFormatInfo for the current culture is used.
如果你不希望在转换失败时处理异常,则可以改为调用 DateTime.TryParse 方法。If you prefer not to handle an exception if the conversion fails, you can call the DateTime.TryParse method instead. 它将返回一个 Boolean 值,该值指示转换是成功还是失败。It returns a Boolean value that indicates whether the conversion succeeded or failed.
适用于
ToDateTime(UInt64)
重要
此 API 不符合 CLS。
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException.
public:
static DateTime ToDateTime(System::UInt64 value);
[System.CLSCompliant(false)]
public static DateTime ToDateTime (ulong value);
public static DateTime ToDateTime (ulong value);
[<System.CLSCompliant(false)>]
static member ToDateTime : uint64 -> DateTime
static member ToDateTime : uint64 -> DateTime
Public Shared Function ToDateTime (value As ULong) As DateTime
参数
- value
- UInt64
要转换的 64 位无符号整数。The 64-bit unsigned integer to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
- 属性
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(Object, IFormatProvider)
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 接口的对象。An object that implements the IConvertible interface.
- provider
- IFormatProvider
一个提供区域性特定的格式设置信息的对象。An object that supplies culture-specific formatting information.
返回
value 的值的日期和时间等效项,如果 MinValue 为 value,则为 null 的日期和时间等效项。The date and time equivalent of the value of value, or the date and time equivalent of MinValue if value is null.
例外
value 是无效的日期和时间值。value is not a valid date and time value.
value 不实现 IConvertible 接口。value does not implement the IConvertible interface.
或-or-
不支持该转换。The conversion is not supported.
示例
下面的示例定义了一个自定义格式提供程序,该提供程序的 CustomProvider GetFormat 方法将消息输出到已调用的控制台,然后返回其 DateTimeFormatInfo 名称作为参数传递给其类构造函数的区域性的对象。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. 其中每个 CustomProvider 对象都用于将对象数组中的元素转换为日期和时间值。Each of these CustomProvider objects is used to convert the elements in an object array to date and time values. 此输出指示 CustomProvider 仅当参数的类型为时,才在转换中使用该对象 value String 。The output indicates that the CustomProvider object is used in the conversion only when the type of the value parameter is a 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
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 。The return value is the result of invoking the IConvertible.ToDateTime method of the underlying type of value.
provider 使用户能够指定有关的内容的特定于区域性的转换信息 value 。provider enables the user to specify culture-specific conversion information about the contents of value. 例如,如果 value 是表示日期的,则 String provider 可能提供有关用于表示该日期的表示法的区域性特定信息。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``value如果的运行时类型 value 是 String ,或者如果 value 是其实现利用的用户定义类型, IConvertible.ToDateTime provider 则涉及转换。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. 如果的运行时类型 value 为 String provider ,则 null CultureInfo 使用表示当前线程区域性的对象。If the runtime type of value is String and provider is null, the CultureInfo object that represents the current thread culture is used.
适用于
ToDateTime(SByte)
重要
此 API 不符合 CLS。
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException.
public:
static DateTime ToDateTime(System::SByte value);
[System.CLSCompliant(false)]
public static DateTime ToDateTime (sbyte value);
public static DateTime ToDateTime (sbyte value);
[<System.CLSCompliant(false)>]
static member ToDateTime : sbyte -> DateTime
static member ToDateTime : sbyte -> DateTime
Public Shared Function ToDateTime (value As SByte) As DateTime
参数
- value
- SByte
要转换的 8 位带符号整数。The 8-bit signed integer to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
- 属性
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(UInt32)
重要
此 API 不符合 CLS。
调用此方法始终引发 InvalidCastException。Calling this method always throws InvalidCastException.
public:
static DateTime ToDateTime(System::UInt32 value);
[System.CLSCompliant(false)]
public static DateTime ToDateTime (uint value);
public static DateTime ToDateTime (uint value);
[<System.CLSCompliant(false)>]
static member ToDateTime : uint32 -> DateTime
static member ToDateTime : uint32 -> DateTime
Public Shared Function ToDateTime (value As UInteger) As DateTime
参数
- value
- UInt32
要转换的 32 位无符号整数。The 32-bit unsigned integer to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
- 属性
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(Object)
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。An object that implements the IConvertible interface, or null.
返回
value 的值的日期和时间等效项,如果 MinValue 为 value,则为 null 的日期和时间等效项。The date and time equivalent of the value of value, or a date and time equivalent of MinValue if value is null.
例外
value 是无效的日期和时间值。value is not a valid date and time value.
value 不实现 IConvertible 接口。value does not implement the IConvertible interface.
或-or-
不支持该转换。The conversion is not supported.
示例
下面的示例 ToDateTime(Object) 使用各种变量来调用方法 Object 。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.
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 必须是 DateTime 或 String ,或者 value 必须是 null 。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. 否则,该方法将引发 InvalidCastException 。Otherwise, the method throws an InvalidCastException. 此外,如果 value 是字符串,则它必须包含当前区域性中的日期和时间值的有效表示形式,否则将 FormatException 引发。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.
返回值是调用的 IConvertible.ToDateTime 基础类型的方法的结果 value 。The return value is the result of invoking the IConvertible.ToDateTime method of the underlying type of value.
适用于
ToDateTime(Double)
调用此方法始终引发 InvalidCastException。Calling this method always throws 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
要转换的双精度浮点值。The double-precision floating-point value to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(Int32)
调用此方法始终引发 InvalidCastException。Calling this method always throws 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 位带符号整数。The 32-bit signed integer to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(Int16)
调用此方法始终引发 InvalidCastException。Calling this method always throws 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 位带符号整数。The 16-bit signed integer to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(Int64)
调用此方法始终引发 InvalidCastException。Calling this method always throws 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 位带符号整数。The 64-bit signed integer to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(Decimal)
调用此方法始终引发 InvalidCastException。Calling this method always throws 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
要转换的数字。The number to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
例外
不支持此转换。This conversion is not supported.
适用于
ToDateTime(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
日期和时间值。A date and time value.
返回
不经更改即返回 value。value is returned unchanged.
适用于
ToDateTime(Char)
调用此方法始终引发 InvalidCastException。Calling this method always throws 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 字符。The Unicode character to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(Byte)
调用此方法始终引发 InvalidCastException。Calling this method always throws 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 位无符号整数。The 8-bit unsigned integer to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
例外
不支持此转换。This conversion is not supported.
另请参阅
适用于
ToDateTime(Boolean)
调用此方法始终引发 InvalidCastException。Calling this method always throws 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
要转换的布尔值。The Boolean value to convert.
返回
不支持此转换。This conversion is not supported. 不返回任何值。No value is returned.
例外
不支持此转换。This conversion is not supported.