In C#, DateTime.GetDateTimeFormats returns a string[]. With no format letter it returns many strings, one for each standard format and each pattern the culture defines. With a letter such as 'd' it returns only the strings for that letter.
It is handy when you want to explore how a date can look, or to build a list of accepted shapes. For normal output, ToString is simpler.
This is an instance member of System.DateTime. You call it on a DateTime value, such as dt.GetDateTimeFormats(...). DateTime is a struct, so every value is copied and never changed in place.
Syntax
Call it with no arguments, with a format character, a culture, or both.
C#
DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
string[] all = dt.GetDateTimeFormats(CultureInfo.InvariantCulture);
string[] dOnly = dt.GetDateTimeFormats('d', CultureInfo.InvariantCulture);
string[] cur = dt.GetDateTimeFormats(); // current culture
Call
What it does
dt.GetDateTimeFormats()
All standard formats for the current culture.
dt.GetDateTimeFormats('D', culture)
Only the long-date strings of a culture.
dt.GetDateTimeFormats(culture)
All standard formats for a chosen culture.
The number and order of strings depend on the culture and the .NET version, so do not hard-code an index or a length. Search the array with Array.IndexOf or Array.Exists.
Library declarations
C#
public string[] GetDateTimeFormats();
public string[] GetDateTimeFormats(char format);
public string[] GetDateTimeFormats(IFormatProvider? provider);
public string[] GetDateTimeFormats(char format, IFormatProvider? provider);
Parameters
Parameter
Description
format
Optional. One standard format character such as 'd', 'D', 's', or 'r'.
provider
Optional. A CultureInfo that decides the patterns and names.
Return Value
Return Value
Description
string[]
An array with one string per format pattern. A letter may produce one or several strings depending on the culture.
C# Examples
Example 1 — Short and Long Date Strings
Ask for the d and D formats with the invariant culture and print each string.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
CultureInfo inv = CultureInfo.InvariantCulture;
foreach (string s in dt.GetDateTimeFormats('d', inv)) Console.WriteLine(s);
foreach (string s in dt.GetDateTimeFormats('D', inv)) Console.WriteLine(s);
}
}
Output
11/18/2026
Wednesday, 18 November 2026
How It Works
Format character. A char such as 'd' selects one standard format.
Array result. The method returns a string[], so a foreach prints every item.
Invariant is simple. That culture defines exactly one pattern for each of these letters.
d and D. The first is the short date and the second is the long date.
Example 2 — Search the Full List
Get every standard format and check that two known strings are inside.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
string[] all = dt.GetDateTimeFormats(CultureInfo.InvariantCulture);
Console.WriteLine(all.Length > 10);
Console.WriteLine(Array.IndexOf(all, "11/18/2026") >= 0);
Console.WriteLine(Array.Exists(all, s => s == "2026-11-18T14:30:45"));
}
}
Output
True
True
True
How It Works
No letter. The call collects strings for all standard format letters.
Length check. Instead of printing the exact count, the code tests that there are more than ten strings.
Short date inside.11/18/2026 comes from the d format.
Sortable inside.2026-11-18T14:30:45 comes from the s format.
Example 3 — Sortable, UTC-style, and RFC 1123
Print the first string for the s, u, and r letters.
C#
using System;
using System.Globalization;
class Program {
static void Main() {
DateTime dt = new DateTime(2026, 11, 18, 14, 30, 45);
CultureInfo inv = CultureInfo.InvariantCulture;
Console.WriteLine(dt.GetDateTimeFormats('s', inv)[0]);
Console.WriteLine(dt.GetDateTimeFormats('u', inv)[0]);
Console.WriteLine(dt.GetDateTimeFormats('r', inv)[0]);
}
}
Output
2026-11-18T14:30:45
2026-11-18 14:30:45Z
Wed, 18 Nov 2026 14:30:45 GMT
How It Works
Index zero. Each of these letters gives one string, so [0] is safe.
s. The sortable pattern puts a T between date and time.
u. The universal sortable pattern ends with Z and uses a space.
r. The RFC 1123 pattern adds the weekday and the word GMT; the value is only formatted, not converted.
Frequently Asked Questions
A string array with the date in many standard formats, or only in one format if you pass a letter.
It depends on the culture and the .NET version, so do not hard-code the length.
Standard format letters such as d, D, f, F, g, G, m, o, r, s, t, T, u, U, and y.
It has a single pattern for most letters, so the output stays the same on every computer.
No. For one known format ToString is simpler. GetDateTimeFormats is for exploring or listing formats.