Format a DateTime as a String in C#: Custom and Standard Formats

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

SpecifierMeaningExample
d, ddDay of the month, 1 to 31 / 01 to 3113
ddd, ddddAbbreviated / full day nameFri, Friday
M, MMMonth, 1 to 12 / 01 to 126, 06
MMM, MMMMAbbreviated / full month nameJun, June
yy, yyyyTwo-digit / four-digit year14, 2014
h, hhHour on a 12-hour clock, 1 to 12 / 01 to 129, 09
H, HHHour on a 24-hour clock, 0 to 23 / 00 to 2321
m, mmMinute, 0 to 59 / 00 to 595, 05
s, ssSecond, 0 to 59 / 00 to 595, 05
f to fffffffFractions of a second, 1 to 7 digitsfff gives 123
F to FFFFFFFLike f, but trailing zeros are omittedFFFF gives 123
t, ttFirst letter of / full AM or PM designatorP, PM
z, zz, zzzOffset from UTC: hours / two-digit hours / hours and minutes+3, +03, +03:00
KTime zone information, depends on DateTime.KindZ, +03:00 or empty
gEraA.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:

FormatOutput for the date aboveUse
o2014-06-13T21:05:05.1230000ISO 8601 round-trip, keeps all 7 fraction digits
s2014-06-13T21:05:05Sortable ISO 8601 without fractions
u2014-06-13 21:05:05ZUniversal sortable
RFri, 13 Jun 2014 21:05:05 GMTRFC 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.

Related examples

Leave a Comment