DateTime.GetDateTimeFormats() Method in C# | Set – 1

This method is used to convert the value of this instance to all the string representations supported by the standard date and time format specifiers. There are total 4 methods in the overload list of this method:

  • GetDateTimeFormats()
  • GetDateTimeFormats(Char)
  • GetDateTimeFormats(IFormatProvider)
  • GetDateTimeFormats(Char, IFormatProvider)

GetDateTimeFormats()

This method is used to convert the value of this instance to all the string representations supported by the standard date and time format specifiers.

Syntax: public string[] GetDateTimeFormats ()

Return Value: This method returns a string array where each element is the representation of the value of this instance formatted with one of the standard date and time format specifiers.

Below programs illustrate the use of GetDateTimeFormats() Method:



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.GetDateTimeFormats()
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of DateTime
        DateTime date = new DateTime(2010, 1,
                                     1, 4, 0, 15);
  
        // getting format in string array
        // using GetDateTimeFormats() method;
        string[] value = date.GetDateTimeFormats();
  
        // Print out value in all DateTime 
        // formats using the default culture.
        foreach(string format in value)
            Console.WriteLine(format);
    }
}

chevron_right


Output:

01/01/2010
2010-01-01
Friday, 01 January 2010
Friday, 01 January 2010 04:00
Friday, 01 January 2010 04:00 AM
Friday, 01 January 2010 4:00
Friday, 01 January 2010 4:00 AM
Friday, 01 January 2010 04:00:15
01/01/2010 04:00
01/01/2010 04:00 AM
01/01/2010 4:00
01/01/2010 4:00 AM
2010-01-01 04:00
2010-01-01 04:00 AM
2010-01-01 4:00
2010-01-01 4:00 AM
01/01/2010 04:00:15
2010-01-01 04:00:15
January 01
January 01
2010-01-01T04:00:15.0000000
2010-01-01T04:00:15.0000000
Fri, 01 Jan 2010 04:00:15 GMT
Fri, 01 Jan 2010 04:00:15 GMT
2010-01-01T04:00:15
04:00
04:00 AM
4:00
4:00 AM
04:00:15
2010-01-01 04:00:15Z
Friday, 01 January 2010 04:00:15
2010 January
2010 January

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.GetDateTimeFormats()
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        // creating object of DateTime
        DateTime date = new DateTime(2019, 1,
                              30, 9, 49, 15);
  
        // getting format in string array
        // using GetDateTimeFormats() method;
        string[] value = date.GetDateTimeFormats();
  
        // Print out value in all DateTime 
        // formats using the default culture.
        for (int i = 1; i <= 6; i++)
            Console.WriteLine(value[i]);
    }
}

chevron_right


Output:

2019-01-30
Wednesday, 30 January 2019
Wednesday, 30 January 2019 09:49
Wednesday, 30 January 2019 09:49 AM
Wednesday, 30 January 2019 9:49
Wednesday, 30 January 2019 9:49 AM

GetDateTimeFormats(Char)

This method is used to convert the value of this instance to all the string representations supported by the specified standard date and time format specifier.

Syntax: public string[] GetDateTimeFormats (char format);
Here it takes a standard date and time format string.

Return Value: This method returns a string array where each element is the representation of the value of this instance formatted with the format standard date and time format specifier.

Exceptions: This method will give FormatException if the format is not a valid standard date and time format specifier character.

Below programs illustrate the use of GetDateTimeFormats(Char) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.GetDateTimeFormats(Char)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
            // creating object of DateTime
            DateTime date = new DateTime(2010, 1,
                                     1, 4, 0, 15);
  
            // Get the long date formats using the current
            // culture. using GetDateTimeFormats() method
            string[] value = date.GetDateTimeFormats('D');
  
            // Print out value in all DateTime
            // formats using the default culture.
            foreach(string format in value)
                Console.WriteLine(format);
        }
        catch (FormatException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

Friday, 01 January 2010

Example 2: For FormatException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// DateTime.GetDateTimeFormats(Char)
// Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
            // creating object of DateTime
            DateTime date = new DateTime(2010, 1,
                                         1, 4, 0, 15);
  
            // Get the date format
            // using GetDateTimeFormats(Char) method;
            string[] value = date.GetDateTimeFormats('X');
  
            // Print out value in all DateTime 
            // formats using the default culture.
            foreach(string format in value)
                Console.WriteLine(format);
        }
        catch (FormatException e) {
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
}

chevron_right


Output:

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.