ZetCode

C# DateTime format

last modified October 5, 2026

In this article we learn how to format DateTime values in C# using standard and custom date and time format strings.

C# DateTime

The DateTime value type represents dates and times with values ranging from 00:00:00 (midnight), January 1, 0001, through 11:59:59 PM, December 31, 9999, in the Gregorian calendar. It does not include a time zone; use DateTimeOffset when an offset is part of the value.

Format methods

To format a DateTime value, we can use DateTime.ToString, String.Format, or an interpolated string such as $"{date:D}". The result depends on the current culture unless a specific CultureInfo is supplied.

C# standard format specifiers

A standard datetime format string uses a single character format specifier to define the text representation of a DateTime value. For instance, the D format specifier denotes a long date format.

SpecifierMeaningExample (en-US culture)
Dlong dateSaturday, March 26, 2022
dshort date03/26/2022
Ffull date and long timeSaturday, March 26, 2022 1:15:12 PM
ffull date and short timeSaturday, March 26, 2022 1:15 PM
Ggeneral date and long time03/26/2022 1:15:12 PM
ggeneral date and short time03/26/2022 1:15 PM
Uuniversal full date and timeSaturday, March 26, 2022 1:15:12 PM
uuniversal sortable date and time2022-03-26 13:15:12Z
ssortable date and time2022-03-26T13:15:12
Tlong time1:15:12 PM
tshort time1:15 PM
Oround-trip date and time2022-03-26T13:15:12.6720000Z
RRFC 1123 date and timeSat, 26 Mar 2022 13:15:12 GMT
Mmonth and dayMarch 26
Yyear and monthMarch 2022

The table lists standard datetime format specifiers in C#.

Program.cs
using System.Globalization;

var date = new DateTime(2022, 3, 26, 13, 15, 12, 672, DateTimeKind.Utc);
var culture = CultureInfo.GetCultureInfo("en-US");

Console.WriteLine(date.ToString("D", culture));
Console.WriteLine(date.ToString("d", culture));
Console.WriteLine(date.ToString("F", culture));
Console.WriteLine(date.ToString("f", culture));
Console.WriteLine(date.ToString("G", culture));
Console.WriteLine(date.ToString("g", culture));
Console.WriteLine(date.ToString("U", culture));
Console.WriteLine(date.ToString("u", culture));
Console.WriteLine(date.ToString("s", culture));
Console.WriteLine(date.ToString("T", culture));
Console.WriteLine(date.ToString("t", culture));
Console.WriteLine(date.ToString("O", culture));
Console.WriteLine(date.ToString("R", culture));
Console.WriteLine(date.ToString("M", culture));
Console.WriteLine(date.ToString("Y", culture));

In the example, we use standard date and time format specifiers with a fixed UTC value and the en-US culture. The U and u formats convert a DateTime value to UTC before formatting it.

$ dotnet run
Saturday, March 26, 2022
03/26/2022
Saturday, March 26, 2022 1:15:12 PM
Saturday, March 26, 2022 1:15 PM
03/26/2022 1:15:12 PM
03/26/2022 1:15 PM
Saturday, March 26, 2022 1:15:12 PM
2022-03-26 13:15:12Z
2022-03-26T13:15:12
1:15:12 PM
1:15 PM
2022-03-26T13:15:12.6720000Z
Sat, 26 Mar 2022 13:15:12 GMT
March 26
March 2022

Program.cs
using System.Globalization;

var date = new DateTime(2022, 3, 26, 13, 15, 12, 672, DateTimeKind.Utc);

Console.WriteLine(FormattableString.Invariant($"{date:D}"));
Console.WriteLine(FormattableString.Invariant($"{date:O}"));
Console.WriteLine(FormattableString.Invariant($"{date:R}"));

Interpolated strings can also contain standard format specifiers. FormattableString.Invariant makes the culture explicit for these examples; ordinary interpolated strings use the current culture.

C# culture-specific standard format specifiers

Pass a CultureInfo to a formatting method when the output must follow a particular culture. This avoids changing the process-wide current culture.

Program.cs
using System.Globalization;

var date = new DateTime(2022, 3, 26, 13, 15, 12, 672, DateTimeKind.Utc);
var culture = CultureInfo.GetCultureInfo("sk-SK");

Console.WriteLine(date.ToString("D", culture));
Console.WriteLine(date.ToString("d", culture));
Console.WriteLine(date.ToString("F", culture));
Console.WriteLine(date.ToString("f", culture));
Console.WriteLine(date.ToString("G", culture));
Console.WriteLine(date.ToString("g", culture));
Console.WriteLine(date.ToString("U", culture));
Console.WriteLine(date.ToString("u", culture));
Console.WriteLine(date.ToString("s", culture));
Console.WriteLine(date.ToString("T", culture));
Console.WriteLine(date.ToString("t", culture));
Console.WriteLine(date.ToString("O", culture));
Console.WriteLine(date.ToString("R", culture));
Console.WriteLine(date.ToString("M", culture));
Console.WriteLine(date.ToString("Y", culture));

In the example, we format the same UTC value with standard specifiers for the Slovak culture. Numeric and textual components follow that culture where the format permits them; invariant formats such as O and R retain their defined layout.

$ dotnet run
sobota 26. marca 2022
26. 3. 2022
sobota 26. marca 2022 13:15:12
sobota 26. marca 2022 13:15
26. 3. 2022 13:15:12
26. 3. 2022 13:15
sobota 26. marca 2022 13:15:12
2022-03-26 13:15:12Z
2022-03-26T13:15:12
13:15:12
13:15
2022-03-26T13:15:12.6720000Z
Sat, 26 Mar 2022 13:15:12 GMT
26. marca
marec 2022

C# custom datetime format specifiers

Custom date and time format specifiers are combined to build a format that is not covered by a standard format string. A format string containing a single custom letter must be escaped or quoted when it could be interpreted as a standard format specifier.

SpecifierMeaning
dThe day of the month, from 1 through 31.
ddThe day of the month, from 01 through 31.
dddThe abbreviated name of the day of the week.
ddddThe full name of the day of the week.
fThe tenths of a second.
ffThe hundredths of a second.
fffThe milliseconds.
ffffThe ten-thousandths of a second.
fffffThe hundred-thousandths of a second.
ffffffThe millionths of a second.
fffffffThe ten-millionths of a second.
g/ggThe period or era.
hThe hour, using a 12-hour clock from 1 to 12.
hhThe hour, using a 12-hour clock from 01 to 12.
HThe hour, using a 24-hour clock from 0 to 23.
HHThe hour, using a 24-hour clock from 00 to 23.
KTime zone information.
mThe minute, from 0 through 59.
mmThe minute, from 00 through 59.
MThe month, from 1 through 12.
MMThe month, from 01 through 12.
MMMThe abbreviated name of the month.
MMMMThe full name of the month.
sThe second, from 0 through 59.
ssThe second, from 00 through 59.
tThe first character of the AM/PM designator.
ttThe AM/PM designator.
yThe year, from 0 to 99.
yyThe year, from 00 to 99.
yyyThe year, with a minimum of three digits.
yyyyThe year as a four-digit number.
yyyyyThe year as a five-digit number.
zHours offset from UTC, with no leading zero.
zzHours offset from UTC, with a leading zero for a single-digit value.
zzzHours and minutes offset from UTC.

The table shows custom date and time format specifiers in C#.

Program.cs
using System.Globalization;

var date = new DateTime(2022, 3, 26, 13, 44, 3, 672, DateTimeKind.Utc);

Console.WriteLine(date.ToString("M/d/yy", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(date.ToString("MM/dd/yyyy", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(date.ToString("yy-MM-dd", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(date.ToString("yy-MMM-dd ddd", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(date.ToString("yyyy-M-d dddd", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(date.ToString("yyyy MMMM dd", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(date.ToString("h:mm:ss tt zzz", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(date.ToString("HH:m:s tt zzz", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(date.ToString("hh:mm:ss t z", CultureInfo.GetCultureInfo("en-US")));
Console.WriteLine(date.ToString("HH:mm:ss tt zz", CultureInfo.GetCultureInfo("en-US")));

In the example, we build several custom date and time formats. The zzz and z results are based on the value's UTC kind.

$ dotnet run
3/26/22
03/26/2022
22-03-26
22-Mar-26 Sat
2022-3-26 Saturday
2022 March 26
1:44:03 PM +00:00
13:44:3 PM +00:00
01:44:03 P +0
13:44:03 PM +00

Parse datetime strings with format specifiers

Both standard and custom format specifiers can be used when parsing date and time strings into DateTime values. ParseExact requires the input to match the supplied format and culture exactly.

Program.cs
using System.Globalization;

var ds = "Thu Nov 11, 2021";
var dt = DateTime.ParseExact(ds, "ddd MMM dd, yyyy", CultureInfo.InvariantCulture);
Console.WriteLine(dt);

var ds2 = "10/22/2021";
var dt2 = DateTime.ParseExact(ds2, "d", CultureInfo.InvariantCulture);
Console.WriteLine(dt2);

In the example, we pass a custom format and a standard format to DateTime.ParseExact. The invariant culture makes the example independent of the machine's current culture.

$ dotnet run
11/11/2021 12:00:00 AM
10/22/2021 12:00:00 AM

Source

Custom date and time format strings

In this article we formatted DateTime values with standard and custom date and time format strings in C#.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all C# tutorials.