Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Date, Time and Globalization

Date and time formatting

Reference for formatting DateTime, DateTimeOffset, DateOnly and TimeOnly in C#: standard specifiers like O, s and d, custom patterns like yyyy-MM-dd, and cultures.

A date and time format string controls how ToString, string interpolation and string.Format turn a date into text. There are two kinds. A standard format is a single letter such as "d", "s" or "O". A custom format is a pattern such as "yyyy-MM-dd HH:mm", built from specifiers like yyyy for the year and MM for the month.

Standard formats other than "o", "O", "s", "u" and "R" depend on the culture, because the order of day and month, month names and the clock style differ between regions. This environment always uses the invariant culture, so pass CultureInfo.InvariantCulture explicitly to show what you mean.

C#
var d = new DateTime(2026, 3, 5, 14, 7, 9);
Console.WriteLine(d.ToString("yyyy-MM-dd HH:mm", CultureInfo.InvariantCulture));   // 2026-03-05 14:07

Try it

C# Standard format specifiers

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Custom patterns and escaping

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
string ToString()
string ToString(string? format)
string ToString(string? format, IFormatProvider? provider)

Standard specifiers (invariant culture shown in the first example above):

Specifier Meaning
d / D Short / long date.
t / T Short / long time.
f / F Long date with short / long time.
g / G Short date with short / long time.
M or m, Y or y Month and day, year and month.
o / O Round-trip ISO 8601 with 7 fraction digits; keeps Kind or offset. Never culture dependent.
s Sortable yyyy-MM-ddTHH:mm:ss, no zone.
u Universal sortable yyyy-MM-dd HH:mm:ssZ, written as-is without conversion.
R / r RFC 1123 (Thu, 05 Mar 2026 14:07:09 GMT), also written as-is.

Custom specifiers:

Specifier Meaning
yy, yyyy Year with 2 or 4 digits.
M, MM, MMM, MMMM Month number, padded number, abbreviated name, full name.
d, dd, ddd, dddd Day number, padded, abbreviated weekday, full weekday.
h, hh, H, HH 12-hour or 24-hour clock, with or without padding.
m, mm, s, ss Minutes and seconds.
f to fffffff, F to FFFFFFF Fractions of a second; F removes trailing zeros.
tt AM/PM designator.
z, zz, zzz, K Offset from UTC; K is Z, an offset or empty depending on the Kind.
'text', \x Literal text.
%x Forces a single character to be read as a custom specifier.

Errors: an unknown single-letter standard format throws FormatException. Specifiers that make no sense for the type (for example HH on a DateOnly) throw FormatException as well, and so does "K" as a single-letter format (write "%K").

Usage notes

  • Quote literal letters. In "yyyy-MM-ddTHH:mm" the T happens to print as itself, but write 'T' so the intent is clear. Letters like h, d or m inside text must be quoted.
  • MM is month, mm is minute. dd/mm/yyyy is a classic bug. hh is the 12-hour clock; for 24-hour use HH.
  • The / and : characters are culture separators. In a custom format / means the culture's date separator and : the time separator. With the invariant culture they print as themselves, but under another culture they may change. Quote them ('/') when the output must be fixed.
  • Machine-readable text uses o or s and the invariant culture, never d or G. Round-trip with DateTime.Parse(text, null, DateTimeStyles.RoundtripKind).
  • u and R do not convert the value to UTC. Call ToUniversalTime() first if your value is local.
  • Culture changes output. The same DateTime formats differently under en-US, de-DE or ja-JP in a normal .NET app; here, non-invariant cultures are unavailable, so those examples must stay read-only.
  • Performance. Formatting allocates a string. For hot paths use TryFormat with a Span<char>.
  • Default ToString() equals "G" for the current culture. Do not rely on it for storage.