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

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 intializing 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);
}
}

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 intializing 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);
}
}

chevron_right


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:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.