DateTime.ToString takes a format string that decides exactly how a date is written. You can build your own layout from custom specifiers such as yyyy, MM and HH, or use a one-letter standard format such as d or o.
The examples use a fixed date so the output is reproducible, and they pass CultureInfo.InvariantCulture. Without that argument, month names, separators and even the calendar come from the current culture of the machine, which is the most common source of “works on my machine” bugs with dates.
Custom formats
using System.Globalization;
var inv = CultureInfo.InvariantCulture;
var date = new DateTime(2014, 6, 13, 21, 5, 5, 123);
Console.WriteLine(date.ToString("yyyy-MM-dd", inv)); // 2014-06-13
Console.WriteLine(date.ToString("yyyy-MM-dd HH:mm:ss", inv)); // 2014-06-13 21:05:05
Console.WriteLine(date.ToString("dd/MM/yyyy", inv)); // 13/06/2014
Console.WriteLine(date.ToString("MMM dd, yyyy", inv)); // Jun 13, 2014
Console.WriteLine(date.ToString("dddd, MMMM d, yyyy", inv)); // Friday, June 13, 2014
Console.WriteLine(date.ToString("ddd d MMM", inv)); // Fri 13 Jun
Console.WriteLine(date.ToString("h:mm tt", inv)); // 9:05 PM
Console.WriteLine(date.ToString("HH:mm:ss.fff", inv)); // 21:05:05.123
Console.WriteLine(date.ToString("yyyyMMdd'T'HHmm", inv)); // 20140613T2105
The same patterns work in interpolated strings, $"{date:yyyy-MM-dd}", and in string.Format("{0:yyyy-MM-dd}", date). Both use the current culture unless you pass one, for example with string.Create(CultureInfo.InvariantCulture, $"{date:yyyy-MM-dd}").
Custom format specifiers
| Specifier | Meaning | Example |
|---|---|---|
d, dd | Day of the month, 1 to 31 / 01 to 31 | 13 |
ddd, dddd | Abbreviated / full day name | Fri, Friday |
M, MM | Month, 1 to 12 / 01 to 12 | 6, 06 |
MMM, MMMM | Abbreviated / full month name | Jun, June |
yy, yyyy | Two-digit / four-digit year | 14, 2014 |
h, hh | Hour on a 12-hour clock, 1 to 12 / 01 to 12 | 9, 09 |
H, HH | Hour on a 24-hour clock, 0 to 23 / 00 to 23 | 21 |
m, mm | Minute, 0 to 59 / 00 to 59 | 5, 05 |
s, ss | Second, 0 to 59 / 00 to 59 | 5, 05 |
f to fffffff | Fractions of a second, 1 to 7 digits | fff gives 123 |
F to FFFFFFF | Like f, but trailing zeros are omitted | FFFF gives 123 |
t, tt | First letter of / full AM or PM designator | P, PM |
z, zz, zzz | Offset from UTC: hours / two-digit hours / hours and minutes | +3, +03, +03:00 |
K | Time zone information, depends on DateTime.Kind | Z, +03:00 or empty |
g | Era | A.D. |
: | Culture’s time separator | |
/ | Culture’s date separator | |
'text' | Literal text, copied as is | 'T' gives T |
\ | Escapes the next character | \h gives h |
% | Marks a single letter as a custom format | %d gives 13 |
Case matters. MM is the month and mm the minute; HH is a 24-hour clock and hh a 12-hour one. Swapping them does not throw, it just gives you wrong dates.
Standard formats
A single letter selects a predefined pattern. Most of them are defined by the culture:
Console.WriteLine(date.ToString("d", inv)); // 06/13/2014
Console.WriteLine(date.ToString("D", inv)); // Friday, 13 June 2014
Console.WriteLine(date.ToString("g", inv)); // 06/13/2014 21:05
Console.WriteLine(date.ToString("t", inv)); // 21:05
var us = CultureInfo.GetCultureInfo("en-US");
var de = CultureInfo.GetCultureInfo("de-DE");
Console.WriteLine(date.ToString("d", us)); // 6/13/2014
Console.WriteLine(date.ToString("d", de)); // 13.06.2014
Use these for text a person will read in their own language; the user’s culture then picks the familiar layout. The culture data comes from the operating system (ICU on Linux and macOS, and on Windows 10 1903 and later for .NET 5+), so long formats for some cultures can differ slightly between machines. Do not compare them in unit tests across platforms.
A few standard formats are culture-independent and meant for machines:
| Format | Output for the date above | Use |
|---|---|---|
o | 2014-06-13T21:05:05.1230000 | ISO 8601 round-trip, keeps all 7 fraction digits |
s | 2014-06-13T21:05:05 | Sortable ISO 8601 without fractions |
u | 2014-06-13 21:05:05Z | Universal sortable |
R | Fri, 13 Jun 2014 21:05:05 GMT | RFC 1123, for HTTP headers |
Note that u and R do not convert the value to UTC. They print the Z or GMT suffix regardless, so call ToUniversalTime() first if your value is local time. o adds Z for a UTC DateTime, the local offset for a local one, and nothing for DateTimeKind.Unspecified (as above).
Culture traps
The / and : in a pattern are not literal characters. They are placeholders for the culture’s separators:
Console.WriteLine(date.ToString("MM/dd/yyyy", de)); // 06.13.2014
Console.WriteLine(date.ToString("MM'/'dd'/'yyyy", de)); // 06/13/2014
Quote them, or use InvariantCulture, when the slashes must stay slashes.
The calendar can change too. In a culture whose default calendar is not Gregorian, such as th-TH (Thai Buddhist calendar), yyyy prints 2557 for 2014. That is right for display to a Thai user and wrong in a file name or a database query.
For anything written to a log, file, URL or API, use InvariantCulture together with o or an explicit yyyy-MM-dd style pattern.
Time zones: prefer DateTimeOffset
The zzz specifier on a DateTime prints the offset of the machine’s local time zone, even when the value is UTC or unspecified. If the offset is part of your data, store it in a DateTimeOffset:
var meeting = new DateTimeOffset(2014, 6, 13, 21, 5, 5, TimeSpan.FromHours(3));
Console.WriteLine(meeting.ToString("yyyy-MM-dd HH:mm:ss zzz", inv)); // 2014-06-13 21:05:05 +03:00
Console.WriteLine(meeting.ToString("o", inv)); // 2014-06-13T21:05:05.0000000+03:00
If you only need a date or a time of day, DateOnly and TimeOnly (.NET 6 and later) take the same format strings and cannot carry a stray time or time zone along.
The full list of specifiers is on Microsoft Learn: Custom date and time format strings.