C# DateTime format
last modified July 5, 2023
In this article we show how to do formatting of DateTime objects in C#.
C# DateTime
The DateTime value type represents dates and times with values
ranging from 00:00:00 (midnight), January 1, 0001 Anno Domini (Common Era)
through 11:59:59 P.M., December 31, 9999 A.D. (C.E.) in the Gregorian calendar.
Format methods
To format DateTime objects, we can use
DateTime.ToString, String.Format or interpolated
string syntax such as $"{now:D}".
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 |
|---|---|---|
| D | long date | Saturday, March 26, 2022 |
| d | short date | 3/26/2022 |
| F | full date long | Saturday, March 26, 2022 12:59:46 PM |
| f | full date short | Saturday, March 26, 2022 12:59 PM |
| G | general long | 3/26/2022 12:59:46 PM |
| g | general short | 3/26/2022 12:59 PM |
| U | universal full | Saturday, March 26, 2022 11:59:46 AM |
| u | universal sortable | 2022-03-26 12:59:46Z |
| s | sortable | 2022-03-26T12:59:46 |
| T | long time | 12:59:46 PM |
| t | short time | 12:59 PM |
| O | ISO 8601 | 2022-03-26T12:59:46.7831825+01:00 |
| R | RFC 1123 | Sat, 26 Mar 2022 12:59:46 GMT |
| M | month | March 26 |
| Y | year month | March 2022 |
The table lists standard datetime format specifiers in C#.
var now = DateTime.Now;
Console.WriteLine(now.ToString("D"));
Console.WriteLine(now.ToString("d"));
Console.WriteLine(now.ToString("F"));
Console.WriteLine(now.ToString("f"));
Console.WriteLine(now.ToString("G"));
Console.WriteLine(now.ToString("g"));
Console.WriteLine(now.ToString("U"));
Console.WriteLine(now.ToString("u"));
Console.WriteLine(now.ToString("s"));
Console.WriteLine(now.ToString("T"));
Console.WriteLine(now.ToString("t"));
Console.WriteLine(now.ToString("O"));
Console.WriteLine(now.ToString("R"));
Console.WriteLine(now.ToString("M"));
Console.WriteLine(now.ToString("Y"));
In the example, we use standard datetime format specifiers in the
DateTime.ToString method to print the current datetime.
$ dotnet run Saturday, March 26, 2022 3/26/2022 Saturday, March 26, 2022 1:15:12 PM Saturday, March 26, 2022 1:15 PM 3/26/2022 1:15:12 PM 3/26/2022 1:15 PM Saturday, March 26, 2022 12: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.6729121+01:00 Sat, 26 Mar 2022 13:15:12 GMT March 26 March 2022
var now = DateTime.Now;
Console.WriteLine($"{now:D}");
Console.WriteLine($"{now:d}");
Console.WriteLine($"{now:F}");
Console.WriteLine($"{now:f}");
Console.WriteLine($"{now:G}");
Console.WriteLine($"{now:g}");
Console.WriteLine($"{now:U}");
Console.WriteLine($"{now:u}");
Console.WriteLine($"{now:s}");
Console.WriteLine($"{now:T}");
Console.WriteLine($"{now:t}");
Console.WriteLine($"{now:O}");
Console.WriteLine($"{now:R}");
Console.WriteLine($"{now:M}");
Console.WriteLine($"{now:Y}");
In the example, we use standard datetime format specifiers inside interpolated strings to print the current datetime.
C# culture-specific standard format specifiers
We can pass the CultureInfo to the formatting method.
using System.Globalization;
var now = DateTime.Now;
var ci = CultureInfo.CreateSpecificCulture("sk-SK");
CultureInfo.DefaultThreadCurrentCulture = ci;
Console.WriteLine(now.ToString("D"));
Console.WriteLine(now.ToString("d"));
Console.WriteLine(now.ToString("F"));
Console.WriteLine(now.ToString("f"));
Console.WriteLine(now.ToString("G"));
Console.WriteLine(now.ToString("g"));
Console.WriteLine(now.ToString("U"));
Console.WriteLine(now.ToString("u"));
Console.WriteLine(now.ToString("s"));
Console.WriteLine(now.ToString("T"));
Console.WriteLine(now.ToString("t"));
Console.WriteLine(now.ToString("O"));
Console.WriteLine(now.ToString("R"));
Console.WriteLine(now.ToString("M"));
Console.WriteLine(now.ToString("Y"));
In the example, we print the current datetime with the standard datetime format specifiers for the Slovak culture.
$ dotnet run sobota 26. marca 2022 26. 3. 2022 sobota 26. marca 2022 13:22:00 sobota 26. marca 2022 13:22 26. 3. 2022 13:22:00 26. 3. 2022 13:22 sobota 26. marca 2022 12:22:00 2022-03-26 13:22:00Z 2022-03-26T13:22:00 13:22:00 13:22 2022-03-26T13:22:00.4524483+01:00 Sat, 26 Mar 2022 13:22:00 GMT 26. marca marec 2022
C# custom datetime format specifiers
Custom datetime format specifiers are additional specifiers that allow us to build our own datetime formats.
| 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 in a date and time value. |
| ff | The hundredths of a second in a date and time value. |
| fff | The milliseconds in a date and time value. |
| ffff | The ten thousandths of a second in a date and time value. |
| fffff | The hundred thousandths of a second in a date and time value.. |
| ffffff | The hundredths of a second in a date and time value. |
| fffffff | The millionths of a second in a date and time value. |
| ffffffff | The ten millionths of a second in a date and time value. |
| 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 zeros. |
| 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 datetime format specifiers in C#.
var now = DateTime.Now;
Console.WriteLine(now.ToString("M/d/yy"));
Console.WriteLine(now.ToString("MM/dd/yyyy"));
Console.WriteLine(now.ToString("yy-MM-dd"));
Console.WriteLine(now.ToString("yy-MMM-dd ddd"));
Console.WriteLine(now.ToString("yyyy-M-d dddd"));
Console.WriteLine(now.ToString("yyyy MMMM dd"));
Console.WriteLine(now.ToString("h:mm:ss tt zzz"));
Console.WriteLine(now.ToString("HH:m:s tt zzz"));
Console.WriteLine(now.ToString("hh:mm:ss t z"));
Console.WriteLine(now.ToString("HH:mm:ss tt zz"));
In the example, we build a few custom datetime formats.
$ 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 +01:00 13:44:3 PM +01:00 01:44:03 P +1 13:44:03 PM +01
Parse datetime strings with format specifiers
Both standard and custom DateTime format specifiers can be used
when parsing datetime strings into DateTime objects.
using System.Globalization; var ds = "Thu Nov 11, 2021"; var dt = DateTime.ParseExact(ds, "ddd MMM dd, yyyy", CultureInfo.CurrentCulture); Console.WriteLine(dt); var ds2 = "10/22/2021"; var dt2 = DateTime.ParseExact(ds2, "d", CultureInfo.CurrentCulture); Console.WriteLine(dt2);
In the example, we pass datetime format specifiers to the
DateTime.ParseExact method.
$ 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 have formatted DateTime objects in C#.
Author
List all C# tutorials.