Converts the value of the specified object to a DateTime object, using the specified culture-specific formatting information.
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
Parameters
An object that supplies culture-specific formatting information.
ReturnsThe date and time equivalent of the value of value
, or the date and time equivalent of DateTime.MinValue if value
is null
.
value
is not a valid date and time value.
value
does not implement the IConvertible interface.
-or-
The conversion is not supported.
ExamplesThe 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.
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
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.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4