DateTime.GetDateTimeFormats() Method in C#


The DateTime.GetDateTimeFormats() method in C# is used to convert the value of this instance to all the string representations supported by the standard date and time format specifiers.

Syntax

Following is the syntax −

public string[] GetDateTimeFormats ()
public string[] GetDateTimeFormats (char ch);

Above, ch is a standard date and time format string.

Example

Let us now see an example to implement the DateTime.GetDateTimeFormats() method −

using System;
public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 11, 10, 7, 20, 45);
      string[] res = d.GetDateTimeFormats();
      foreach(string s in res)
      Console.WriteLine(s);
   }
}

Output

This will produce the following output −

11/10/2019
11/10/19
11/10/19
11/10/2019
19/11/10
2019-11-10
10-Nov-19
Sunday, November 10, 2019
November 10, 2019
Sunday, 10 November, 2019
10 November, 2019
Sunday, November 10, 2019 7:20 AM
Sunday, November 10, 2019 07:20 AM
Sunday, November 10, 2019 7:20
Sunday, November 10, 2019 07:20
November 10, 2019 7:20 AM
November 10, 2019 07:20 AM
November 10, 2019 7:20
November 10, 2019 07:20
Sunday, 10 November, 2019 7:20 AM
Sunday, 10 November, 2019 07:20 AM
Sunday, 10 November, 2019 7:20
Sunday, 10 November, 2019 07:20
10 November, 2019 7:20 AM
10 November, 2019 07:20 AM
10 November, 2019 7:20
10 November, 2019 07:20
Sunday, November 10, 2019 7:20:45 AM
Sunday, November 10, 2019 07:20:45 AM
Sunday, November 10, 2019 7:20:45
Sunday, November 10, 2019 07:20:45
November 10, 2019 7:20:45 AM
November 10, 2019 07:20:45 AM
November 10, 2019 7:20:45
November 10, 2019 07:20:45
Sunday, 10 November, 2019 7:20:45 AM
Sunday, 10 November, 2019 07:20:45 AM
Sunday, 10 November, 2019 7:20:45
Sunday, 10 November, 2019 07:20:45
10 November, 2019 7:20:45 AM
10 November, 2019 07:20:45 AM
10 November, 2019 7:20:45
10 November, 2019 07:20:45
11/10/2019 7:20 AM
11/10/2019 07:20 AM
11/10/2019 7:20
11/10/2019 07:20
11/10/19 7:20 AM
11/10/19 07:20 AM
11/10/19 7:20
11/10/19 07:20
11/10/19 7:20 AM
11/10/19 07:20 AM
11/10/19 7:20
11/10/19 07:20
11/10/2019 7:20 AM
11/10/2019 07:20 AM
11/10/2019 7:20
11/10/2019 07:20
19/11/10 7:20 AM
19/11/10 07:20 AM
19/11/10 7:20
19/11/10 07:20
2019-11-10 7:20 AM
2019-11-10 07:20 AM
2019-11-10 7:20
2019-11-10 07:20
10-Nov-19 7:20 AM
10-Nov-19 07:20 AM
10-Nov-19 7:20
10-Nov-19 07:20
11/10/2019 7:20:45 AM
11/10/2019 07:20:45 AM
11/10/2019 7:20:45
11/10/2019 07:20:45
11/10/19 7:20:45 AM
11/10/19 07:20:45 AM
11/10/19 7:20:45
11/10/19 07:20:45
11/10/19 7:20:45 AM
11/10/19 07:20:45 AM
11/10/19 7:20:45
11/10/19 07:20:45
11/10/2019 7:20:45 AM
11/10/2019 07:20:45 AM
11/10/2019 7:20:45
11/10/2019 07:20:45
19/11/10 7:20:45 AM
19/11/10 07:20:45 AM
19/11/10 7:20:45
19/11/10 07:20:45
2019-11-10 7:20:45 AM
2019-11-10 07:20:45 AM
2019-11-10 7:20:45
2019-11-10 07:20:45
10-Nov-19 7:20:45 AM
10-Nov-19 07:20:45 AM
10-Nov-19 7:20:45
10-Nov-19 07:20:45
November 10
November 10
2019-11-10T07:20:45.0000000
2019-11-10T07:20:45.0000000
Sun, 10 Nov 2019 07:20:45 GMT
Sun, 10 Nov 2019 07:20:45 GMT
2019-11-10T07:20:45
7:20 AM
07:20 AM
7:20
07:20
7:20:45 AM
07:20:45 AM
7:20:45
07:20:45
2019-11-10 07:20:45Z
Sunday, November 10, 2019 7:20:45 AM
Sunday, November 10, 2019 07:20:45 AM
Sunday, November 10, 2019 7:20:45
Sunday, November 10, 2019 07:20:45
November 10, 2019 7:20:45 AM
November 10, 2019 07:20:45 AM
November 10, 2019 7:20:45
November 10, 2019 07:20:45
Sunday, 10 November, 2019 7:20:45 AM
Sunday, 10 November, 2019 07:20:45 AM
Sunday, 10 November, 2019 7:20:45
Sunday, 10 November, 2019 07:20:45
10 November, 2019 7:20:45 AM
10 November, 2019 07:20:45 AM
10 November, 2019 7:20:45
10 November, 2019 07:20:45
November 2019
November 2019

Example

Let us now see another example to implement the DateTime.GetDateTimeFormats() method. For char format, we use format specifiers like "d" for Short date pattern, "D" for Long date pattern, “F” for Full date/time pattern (long time), etc,

using System;
public class Demo {
   public static void Main() {
      DateTime d = new DateTime(2019, 11, 10, 7, 20, 45);
      string[] res = d.GetDateTimeFormats('F');
      foreach(string s in res)
      Console.WriteLine(s);
   }
}

Output

This will produce the following output −

Sunday, November 10, 2019 7:20:45 AM
Sunday, November 10, 2019 07:20:45 AM
Sunday, November 10, 2019 7:20:45
Sunday, November 10, 2019 07:20:45
November 10, 2019 7:20:45 AM
November 10, 2019 07:20:45 AM
November 10, 2019 7:20:45
November 10, 2019 07:20:45
Sunday, 10 November, 2019 7:20:45 AM
Sunday, 10 November, 2019 07:20:45 AM
Sunday, 10 November, 2019 7:20:45
Sunday, 10 November, 2019 07:20:45
10 November, 2019 7:20:45 AM
10 November, 2019 07:20:45 AM
10 November, 2019 7:20:45
10 November, 2019 07:20:45
raja
Published on 08-Nov-2019 10:33:30
Advertisements