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.
| Specifier | Meaning | Example (en-US culture) |
|---|---|---|
| D | long date | Saturday, March 26, 2022 |
| d | short date | 03/26/2022 |
| F | full date and long time | Saturday, March 26, 2022 1:15:12 PM |
| f | full date and short time | Saturday, March 26, 2022 1:15 PM |
| G | general date and long time | 03/26/2022 1:15:12 PM |
| g | general date and short time | 03/26/2022 1:15 PM |
| U | universal full date and time | Saturday, March 26, 2022 1:15:12 PM |
| u | universal sortable date and time | 2022-03-26 13:15:12Z |
| s | sortable date and time | 2022-03-26T13:15:12 |
| T | long time | 1:15:12 PM |
| t | short time | 1:15 PM |
| O | round-trip date and time | 2022-03-26T13:15:12.6720000Z |
| R | RFC 1123 date and time | Sat, 26 Mar 2022 13:15:12 GMT |
| M | month and day | March 26 |
| Y | year and month | March 2022 |
The table lists standard datetime format specifiers in C#.
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
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.
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.
| Specifier | Meaning |
|---|---|
| d | The day of the month, from 1 through 31. |
| dd | The day of the month, from 01 through 31. |
| ddd | The abbreviated name of the day of the week. |
| dddd | The full name of the day of the week. |
| f | The tenths of a second. |
| ff | The hundredths of a second. |
| fff | The milliseconds. |
| ffff | The ten-thousandths of a second. |
| fffff | The hundred-thousandths of a second. |
| ffffff | The millionths of a second. |
| fffffff | The ten-millionths of a second. |
| g/gg | The period or era. |
| h | The hour, using a 12-hour clock from 1 to 12. |
| hh | The hour, using a 12-hour clock from 01 to 12. |
| H | The hour, using a 24-hour clock from 0 to 23. |
| HH | The hour, using a 24-hour clock from 00 to 23. |
| K | Time zone information. |
| m | The minute, from 0 through 59. |
| mm | The minute, from 00 through 59. |
| M | The month, from 1 through 12. |
| MM | The month, from 01 through 12. |
| MMM | The abbreviated name of the month. |
| MMMM | The full name of the month. |
| s | The second, from 0 through 59. |
| ss | The second, from 00 through 59. |
| t | The first character of the AM/PM designator. |
| tt | The AM/PM designator. |
| y | The year, from 0 to 99. |
| yy | The year, from 00 to 99. |
| yyy | The year, with a minimum of three digits. |
| yyyy | The year as a four-digit number. |
| yyyyy | The year as a five-digit number. |
| z | Hours offset from UTC, with no leading zero. |
| zz | Hours offset from UTC, with a leading zero for a single-digit value. |
| zzz | Hours and minutes offset from UTC. |
The table shows custom date and time format specifiers in C#.
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.
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
List all C# tutorials.