This is a reference article. The next table will contain all standard DateTime formats in the C# with examples:
Date Format | Description |
---|---|
DateTime.Now.ToString(“MM/dd/yyyy”) | 05/29/2022 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy”) | Friday, 29 May 2022 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy”) | Friday, 29 May 2022 05:50 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy”) | Friday, 29 May 2022 05:50 AM |
DateTime.Now.ToString(“dddd, dd MMMM yyyy”) | Friday, 29 May 2022 5:50 |
DateTime.Now.ToString(“dddd, dd MMMM yyyy”) | Friday, 29 May 2022 5:50 AM |
DateTime.Now.ToString(“dddd, dd MMMM yyyy HH:mm:ss”) | Friday, 29 May 2022 05:50:06 |
DateTime.Now.ToString(“MM/dd/yyyy HH:mm”) | 05/29/2022 05:50 |
DateTime.Now.ToString(“MM/dd/yyyy hh:mm tt”) | 05/29/2022 05:50 AM |
DateTime.Now.ToString(“MM/dd/yyyy H:mm”) | 05/29/2022 5:50 |
DateTime.Now.ToString(“MM/dd/yyyy h:mm tt”) | 05/29/2022 5:50 AM |
DateTime.Now.ToString(“MM/dd/yyyy HH:mm:ss”) | 05/29/2022 05:50:06 |
DateTime.Now.ToString(“MMMM dd”) | May 29 |
DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK”) | 2022-05-16T05:50:06.7199222-04:00 |
DateTime.Now.ToString(“ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’”) | Fri, 16 May 2022 05:50:06 GMT |
DateTime.Now.ToString(“yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss”) | 2022-05-16T05:50:06 |
DateTime.Now.ToString(“HH:mm”) | 05:50 |
DateTime.Now.ToString(“hh:mm tt”) | 05:50 AM |
DateTime.Now.ToString(“H:mm”) | 5:50 |
DateTime.Now.ToString(“h:mm tt”) | 5:50 AM |
DateTime.Now.ToString(“HH:mm:ss”) | 05:50:06 |
DateTime.Now.ToString(“yyyy MMMM”) | 2022 May |
How to set Custom Format?
A number of string formats are used to format date and time output.
Format | Description |
---|---|
d | Day of the month. 1 to 31 (no leading zero) |
dd | Day of the month. 01 to 31 (with leading zero) |
ddd | Short name of the day of the week |
dddd | Full name of the day of the week |
M | Month. 1 to 12 (no leading zero) |
MM | Month. 01 to 12 (with leading zero) |
MMM | Brief name of the month |
MMMM | Full month name |
y | One or two last digits of the year (almost irrelevant) |
yy | The last two digits of the year |
yyy | The last three digits of the year |
yyyy | The last four digits of the year |
yyyyy | The last five digits of the year (will not be soon 🙂 ) |
h | Hour. 0 to 12. (no leading zero) |
hh | Hour. 00 to 12 (with leading zero) |
H | Hour. 0 to 23 (no leading zero) |
HH | Hour. 00 to 23 (with leading zero) |
m | Minute. 0 to 59 (no leading zero) |
mm | Minute. 00 to 59 (with leading zero) |
s | Second. 0 to 59 (no leading zero) |
ss | Second. 00 to 59 (with leading zero) |
from f to fffffff | Milliseconds. The displayed number of digits corresponds to the number of “f” |
K | TimeZone |
t | Half day. A or P |
tt | Half day. AM or PM |
z | Offset in hours from GMT (no leading zero) |
zz | Offset in hours from GMT (with leading zero) |
g | Period or era |
R | GMT(Time by Greenwich) |
Here is a code snippet with standard DateTime formats
DateTime today = DateTime.Now; Console.WriteLine($"D: {today.ToString("D")}"); Console.WriteLine($"d: {today.ToString("d")}"); Console.WriteLine($"F: {today.ToString("F")}"); Console.WriteLine($"f: {today:f}"); Console.WriteLine($"G: {today:G}"); Console.WriteLine($"g: {today:g}"); Console.WriteLine($"M: {today:M}"); Console.WriteLine($"O: {today:O}"); Console.WriteLine($"o: {today:o}"); Console.WriteLine($"R: {today:R}"); Console.WriteLine($"s: {today:s}"); Console.WriteLine($"T: {today:T}"); Console.WriteLine($"t: {today:t}"); Console.WriteLine($"U: {today:U}"); Console.WriteLine($"u: {today:u}"); Console.WriteLine($"Y: {today:Y}"); Console.ReadLine();
Result: