DateTime.GetDateTimeFormats() Method in C#

The DateTime.GetDateTimeFormats() method in C# converts a DateTime instance to all string representations supported by the standard date and time format specifiers. This method is useful for discovering all possible formatted outputs for a given DateTime value.

Syntax

Following are the two overloads of the method −

public string[] GetDateTimeFormats()
public string[] GetDateTimeFormats(char format)

Parameters

  • format − A standard date and time format character that specifies which format patterns to return (optional).

Return Value

Returns a string[] containing all possible string representations of the DateTime value using the standard format specifiers.

Using GetDateTimeFormats() Without Parameters

When called without parameters, this method returns all possible formatted strings for the DateTime instance −

using System;

public class Demo {
    public static void Main() {
        DateTime d = new DateTime(2019, 11, 10, 7, 20, 45);
        string[] res = d.GetDateTimeFormats();
        Console.WriteLine("Total formats: " + res.Length);
        Console.WriteLine("First 10 formats:");
        for (int i = 0; i < 10; i++) {
            Console.WriteLine(res[i]);
        }
    }
}

The output of the above code is −

Total formats: 119
First 10 formats:
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

Using GetDateTimeFormats() With Format Specifier

You can filter the results by specifying a format character. Common format specifiers include 'd' for short date, 'D' for long date, 'F' for full date/time, and 't' for short time −

using System;

public class Demo {
    public static void Main() {
        DateTime d = new DateTime(2019, 11, 10, 7, 20, 45);
        
        Console.WriteLine("Short date patterns ('d'):");
        string[] shortDates = d.GetDateTimeFormats('d');
        foreach (string s in shortDates) {
            Console.WriteLine(s);
        }
        
        Console.WriteLine("\nShort time patterns ('t'):");
        string[] shortTimes = d.GetDateTimeFormats('t');
        foreach (string s in shortTimes) {
            Console.WriteLine(s);
        }
    }
}

The output of the above code is −

Short date patterns ('d'):
11/10/2019
11/10/19
11/10/19
11/10/2019
19/11/10
2019-11-10
10-Nov-19

Short time patterns ('t'):
7:20 AM
07:20 AM
7:20
07:20

Common Format Specifiers

Format Character Description Example Output
d Short date pattern 11/10/2019
D Long date pattern Sunday, November 10, 2019
F Full date/time pattern (long time) Sunday, November 10, 2019 7:20:45 AM
t Short time pattern 7:20 AM
T Long time pattern 7:20:45 AM

Practical Example

using System;

public class Demo {
    public static void Main() {
        DateTime now = DateTime.Now;
        
        Console.WriteLine("Full date/time formats ('F'):");
        string[] fullFormats = now.GetDateTimeFormats('F');
        Console.WriteLine("Count: " + fullFormats.Length);
        Console.WriteLine("Sample: " + fullFormats[0]);
        
        Console.WriteLine("\nLong date formats ('D'):");
        string[] longDates = now.GetDateTimeFormats('D');
        Console.WriteLine("Count: " + longDates.Length);
        Console.WriteLine("Sample: " + longDates[0]);
    }
}

The output of the above code is −

Full date/time formats ('F'):
Count: 16
Sample: Sunday, November 10, 2019 7:20:45 AM

Long date formats ('D'):
Count: 4
Sample: Sunday, November 10, 2019

Conclusion

The DateTime.GetDateTimeFormats() method provides a comprehensive way to discover all possible string representations of a DateTime value. Use the parameterless version to see all formats, or specify a format character to filter results for specific pattern types like dates, times, or combined date/time formats.

Updated on: 2026-03-17T07:04:35+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements