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.
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
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.
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
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"theThappens to print as itself, but write'T'so the intent is clear. Letters likeh,dorminside text must be quoted. MMis month,mmis minute.dd/mm/yyyyis a classic bug.hhis the 12-hour clock; for 24-hour useHH.- 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
oorsand the invariant culture, neverdorG. Round-trip withDateTime.Parse(text, null, DateTimeStyles.RoundtripKind). uandRdo not convert the value to UTC. CallToUniversalTime()first if your value is local.- Culture changes output. The same
DateTimeformats differently underen-US,de-DEorja-JPin 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
TryFormatwith aSpan<char>. - Default
ToString()equals"G"for the current culture. Do not rely on it for storage.