C# | Convert.ToDateTime(String, IFormatProvider) Method
Last Updated :
11 Jul, 2025
This method is used to convert the specified string representation of a number to an equivalent date and time, using the specified culture-specific formatting information.
Syntax:
public static DateTime ToDateTime (string value, IFormatProvider provider);
Parameters:
- value: A string that contains a date and time to convert.
- provider: An object that supplies culture-specific formatting information.
Return Value: This method returns the date and time equivalent of the value of value, or the date and time equivalent of MinValue if the value is null.
Exception: This method will give FormatException if the value is not a properly formatted date and time string.
Below programs illustrate the use of Convert.ToDateTime(String, IFormatProvider) Method
Example 1:
csharp
// C# program to demonstrate the
// Convert.ToDateTime() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and initializing String array
string[] values = {"01/02/09", "2009/02/03",
"01/2009/03", "01/02/2009", "01/02/23"};
// calling get() Method
Console.WriteLine("Converted string value"+
" to specified DateTime: ");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
}
catch (FormatException e) {
Console.WriteLine("\n");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s,
CultureInfo cultures)
{
// converting string to specified bool
DateTime val = Convert.ToDateTime(s, cultures);
// display the converted string
Console.Write(" {0}, ", val);
}
}
Output:
Converted string value to specified DateTime:
01/02/2009 00:00:00, 02/03/2009 00:00:00, 01/03/2009 00:00:00, 01/02/2009 00:00:00, 01/02/2023 00:00:00,
Example 2: For FormatException
csharp
// C# program to demonstrate the
// Convert.ToDateTime() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and initializing String array
string[] values = {"01/02/09", "2009/02/03",
"01/2009/03", "01/02/2009", "01/02/23"};
// calling get() Method
Console.WriteLine("Converted string value "+
"to specified DateTime: ");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
Console.WriteLine("\n\nvalue is not a properly "+
"formatted date and time string.");
// converting string to specified bool
DateTime val = Convert.ToDateTime("21/2009/03",
cultures);
// display the converted string
Console.Write(" {0}, ", val);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s,
CultureInfo cultures)
{
// converting string to specified bool
DateTime val = Convert.ToDateTime(s, cultures);
// display the converted string
Console.Write(" {0}, ", val);
}
}
Output:
Converted string value to specified DateTime:
01/02/2009 00:00:00, 02/03/2009 00:00:00, 01/03/2009 00:00:00, 01/02/2009 00:00:00, 01/02/2023 00:00:00,
value is not a properly formatted date and time string.
Exception Thrown: System.FormatException
Reference: