C# DateTime GetDateTimeFormats() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DateTime

Definition

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
CallWhat 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

ParameterDescription
formatOptional. One standard format character such as 'd', 'D', 's', or 'r'.
providerOptional. A CultureInfo that decides the patterns and names.

Return Value

Return ValueDescription
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);
    }
}

How It Works

  1. Format character. A char such as 'd' selects one standard format.
  2. Array result. The method returns a string[], so a foreach prints every item.
  3. Invariant is simple. That culture defines exactly one pattern for each of these letters.
  4. 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"));
    }
}

How It Works

  1. No letter. The call collects strings for all standard format letters.
  2. Length check. Instead of printing the exact count, the code tests that there are more than ten strings.
  3. Short date inside. 11/18/2026 comes from the d format.
  4. 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]);
    }
}

How It Works

  1. Index zero. Each of these letters gives one string, so [0] is safe.
  2. s. The sortable pattern puts a T between date and time.
  3. u. The universal sortable pattern ends with Z and uses a space.
  4. 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.

Next: GetHashCode()

Get an int hash code for a DateTime, used by HashSet and Dictionary with System.DateTime.

GetHashCode() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

15 people found this page helpful