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 valuesranging 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 useDateTime.ToString
, String.Format
or interpolatedstring syntax such as $"{now:D}"
.
C# standard format specifiers
A standard datetime format string uses a single character format specifier todefine the text representation of a DateTime value. For instance, theD
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#.
Program.cs
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 theDateTime.ToString
method to print the current datetime.
$ dotnet run Saturday, March 26, 20223/26/2022Saturday, March 26, 2022 1:15:12 PMSaturday, March 26, 2022 1:15 PM3/26/2022 1:15:12 PM3/26/2022 1:15 PMSaturday, March 26, 2022 12:15:12 PM2022-03-26 13:15:12Z2022-03-26T13:15:121:15:12 PM1:15 PM2022-03-26T13:15:12.6729121+01:00Sat, 26 Mar 2022 13:15:12 GMTMarch 26March 2022
Program.cs
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 interpolatedstrings to print the current datetime.
C# culture-specific standard format specifiers
We can pass the CultureInfo
to the formatting method.
Program.cs
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 formatspecifiers for the Slovak culture.
$ dotnet run sobota 26. marca 202226. 3. 2022sobota 26. marca 2022 13:22:00sobota 26. marca 2022 13:2226. 3. 2022 13:22:0026. 3. 2022 13:22sobota 26. marca 2022 12:22:002022-03-26 13:22:00Z2022-03-26T13:22:0013:22:0013:222022-03-26T13:22:00.4524483+01:00Sat, 26 Mar 2022 13:22:00 GMT26. marcamarec 2022
C# custom datetime format specifiers
Custom datetime format specifiers are additional specifiers that allow usto 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#.
Program.cs
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 run3/26/2203/26/202222-03-2622-Mar-26 Sat2022-3-26 Saturday2022 March 261:44:03 PM +01:0013:44:3 PM +01:0001:44:03 P +113:44:03 PM +01
Parse datetime strings with format specifiers
Both standard and custom DateTime
format specifiers can be usedwhen parsing datetime strings into DateTime
objects.
Program.cs
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 theDateTime.ParseExact
method.
$ dotnet run11/11/2021 12:00:00 AM10/22/2021 12:00:00 AM
Source
Custom date and time format strings
In this article we have formatted DateTime objects in C#.
Author
My name is Jan Bodnar and I am a passionate programmer with many years ofprogramming experience. I have been writing programming articles since 2007. Sofar, I have written over 1400 articles and 8 e-books. I have over eight years ofexperience in teaching programming.
List all C# tutorials.