C# DateTime.ToString Format Strings
Reference for C# DateTime.ToString custom format strings. Covers standard and custom format specifiers, culture-specific formatting, and DateTimeOffset handling.
Detailed Explanation
C# DateTime.ToString Format Strings
C# provides both standard (single-character) and custom (multi-character) format specifiers for DateTime.ToString(). The custom format strings use a token system similar to Java's DateTimeFormatter.
Custom Format Specifiers
| Specifier | Meaning | Example |
|---|---|---|
yyyy |
Year (4-digit) | 2026 |
yy |
Year (2-digit) | 26 |
MMMM |
Full month name | February |
MMM |
Abbreviated month | Feb |
MM |
Month (zero-padded) | 02 |
M |
Month (no padding) | 2 |
dddd |
Full weekday | Saturday |
ddd |
Abbreviated weekday | Sat |
dd |
Day (zero-padded) | 28 |
d |
Day (no padding) | 28 |
HH |
Hour 24h (zero-padded) | 14 |
H |
Hour 24h (no padding) | 14 |
hh |
Hour 12h (zero-padded) | 02 |
h |
Hour 12h (no padding) | 2 |
mm |
Minute (zero-padded) | 30 |
m |
Minute (no padding) | 30 |
ss |
Second (zero-padded) | 00 |
fff |
Milliseconds | 123 |
tt |
AM/PM | PM |
zzz |
Offset (+HH:mm) | +09:00 |
K |
Timezone info | +09:00 or Z |
Standard Format Specifiers
Single-character shortcuts that expand to culture-aware patterns:
| Specifier | Name | en-US Example |
|---|---|---|
d |
Short date | 2/28/2026 |
D |
Long date | Saturday, February 28, 2026 |
t |
Short time | 2:30 PM |
T |
Long time | 2:30:00 PM |
f |
Full (short time) | Saturday, February 28, 2026 2:30 PM |
F |
Full (long time) | Saturday, February 28, 2026 2:30:00 PM |
g |
General (short time) | 2/28/2026 2:30 PM |
G |
General (long time) | 2/28/2026 2:30:00 PM |
o |
Round-trip | 2026-02-28T14:30:00.0000000+09:00 |
s |
Sortable | 2026-02-28T14:30:00 |
u |
Universal sortable | 2026-02-28 14:30:00Z |
r/R |
RFC 1123 | Sat, 28 Feb 2026 14:30:00 GMT |
Usage Examples
DateTime dt = new DateTime(2026, 2, 28, 14, 30, 0);
dt.ToString("yyyy-MM-dd"); // "2026-02-28"
dt.ToString("yyyy-MM-dd HH:mm:ss"); // "2026-02-28 14:30:00"
dt.ToString("MMMM d, yyyy"); // "February 28, 2026"
dt.ToString("ddd, dd MMM yyyy"); // "Sat, 28 Feb 2026"
dt.ToString("hh:mm tt"); // "02:30 PM"
dt.ToString("o"); // Round-trip format
// Culture-specific
dt.ToString("d", new CultureInfo("de-DE")); // "28.02.2026"
dt.ToString("d", new CultureInfo("ja-JP")); // "2026/02/28"
Escaping Literal Characters
Use single quotes or backslash to include literal characters:
dt.ToString("'Date:' yyyy-MM-dd"); // "Date: 2026-02-28"
dt.ToString("yyyy\-MM\-dd"); // "2026-02-28"
DateTimeOffset
For timezone-aware dates, use DateTimeOffset:
DateTimeOffset dto = new DateTimeOffset(2026, 2, 28, 14, 30, 0,
TimeSpan.FromHours(9));
dto.ToString("yyyy-MM-ddTHH:mm:sszzz"); // "2026-02-28T14:30:00+09:00"
dto.ToString("o"); // Full precision round-trip
Use Case
C# DateTime formatting is used in ASP.NET Web API responses, Entity Framework query results, Windows Forms/WPF UI bindings, Azure Functions logging, Blazor component rendering, NLog/Serilog log output patterns, and .NET MAUI mobile app date displays.