C# | Convert.ToDateTime(String, IFormatProvider) Method

02 Sep, 2021
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) MethodExample 1: 
csharp
// C# program to demonstrate the// Convert.ToDateTime() Methodusing System;using System.Globalization; class GFG { // Main Methodpublic 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() methodpublic 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);}}
Example 2: For FormatException
csharp
// C# program to demonstrate the// Convert.ToDateTime() Methodusing System;using System.Globalization;class GFG { // Main Methodpublic 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() methodpublic 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);}}
Reference: 
https://docs.microsoft.com/en-us/dotnet/api/system.convert.todatetime?view=netframework-4.7.2#System_Convert_ToDateTime_System_String_System_IFormatProvider_ 
sweetyty
CSharp Convert Class
CSharp-method
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.