TechieClues TechieClues
Updated date Feb 07, 2022
In this blog, we will see how to convert DateTime into various string formates in C#.
  • 15.1k
  • 0
  • 0

The below examples show how to format DateTime using String.Format method, Standard formats can be used to convert DateTime into the string format.

DateTime dt = DateTime.Now;

string.Format("{0:MM/dd/yyyy}", dt)                                  // 06/12/2020
string.Format("{0:dd-MM-yyyy}", dt)                                  // 12/06/2020
string.Format("{0:MMM dd, yyyy}", dt)                                // Jun 12, 2020
string.Format("{0:dddd, dd MMMM yyyy H:mm}", dt)                     // Friday, 12 June 2020 16:06
string.Format("{0:dddd, dd MMMM yyyy h:mm tt}", dt)                  // Friday, 12 June 2020 4:06 PM
string.Format("{0:dddd, dd MMMM yyyy HH:mm}", dt)                    // Friday, 12 June 2020 16:06
string.Format("{0:dddd, dd MMMM yyyy hh:mm tt}", dt)                 // Friday, 12 June 2020 04:06 PM
string.Format("{0:dddd, dd MMMM yyyy HH:mm:ss}", dt)                 // Friday, 12 June 2020 16:06:55
string.Format("{0:MM/dd/yyyy HH:mm}", dt)                            // 06/12/2020 16:06
string.Format("{0:MM/dd/yyyy hh:mm tt}", dt)                         // 06/12/2020 04:06 PM
string.Format("{0:MM/dd/yyyy H:mm}", dt)                             // 06/12/2020 16:06
string.Format("{0:MM/dd/yyyy h:mm tt}", dt)                          // 06/12/2020 4:06 PM
string.Format("{0:MM/dd/yyyy HH:mm:ss}", dt)                         // 06/12/2020 16:06:55
string.Format("{0:MMMM dd}", dt)                                     // June 12
string.Format("{0:HH:mm}", dt)                                       // 16:06
string.Format("{0:hh:mm tt}", dt)                                    // 04:06 PM
string.Format("{0:H:mm}", dt)                                        // 16:06
string.Format("{0:h:mm tt}", dt)                                     // 4:06 PM
string.Format("{0:HH:mm:ss}", dt)                                    // 16:06:55
string.Format("{0:yyyy MMMM}", dt)                                   // 2020 June
string.Format("{0:yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss.fffffffK}", dt)      // 2020'-'06'-'12'T'16':'06':'55.7673403+10:00
string.Format("{0:ddd, dd MMM yyy HH’:’mm’:’ss ‘GMT’}", dt)          // Fri, 12 Jun 2020 16':'06':'55 'G6T'
string.Format("{0:yyyy’-‘MM’-‘dd’T’HH’:’mm’:’ss}", dt)               // 2020'-'06'-'12'T'16':'06':'55

The DateTime string format can be also done using DateTime.ToString() method. For example,

DateTime.Now.ToString("MM/dd/yyyy")                                   // 06/12/2020
DateTime.Now.ToString("dd-MM-yyyy")                                   // 12/06/2020
DateTime.Now.ToString("MMM dd, yyyy")                                 // Jun 12, 2020
DateTime.Now.ToString("dddd, dd MMMM yyyy H:mm")                      // Friday, 12 June 2020 16:06
DateTime.Now.ToString("MMMM dd")                                      // June 12
DateTime.Now.ToString("HH:mm")                                        // 16:06
DateTime.Now.ToString("hh:mm tt")                                     // 04:06 PM

Standard date and time format strings:

Format specifier Description Pattern (Usage) Pattern Output
d Short date {0:d} M/d/yyyy 6/12/2020
D Long date  {0:D} dddd, MMMM dd, yyyy Tuesday, June 12, 2020
f Full date/time (a combination of D and t) {0:f} dddd, MMMM dd, yyyy h:mm tt Tuesday, June 12, 2020 8:30 PM
F Full date/time  (long time). {0:F} dddd, MMMM dd, yyyy h:mm:ss tt Tuesday, June 12, 2020 8:30:22 PM
g General date/time   (combination of d and t) {0:g} M/d/yyyy h:mm tt 6/12/2020 8:30 PM
G General date/time (combination of d and T) {0:G} M/d/yyyy h:mm:ss tt 6/12/2020 8:30:22 PM
m, M Month/Day  {0:M} or {0:m} MMMM dd June 12
y, Y Year/Month  {0:Y} or {0:y} MMMM, yyyy June, 2020
r, R RFC1123  {0:R} or {0:r} ddd, dd MMM yyyy HH':'mm':'ss 'GMT'  Tue, 12 Jun 2020 20:30:22 GMT
t Short time  {0:t} h:mm tt 8:30 PM
T Long time  {0:T} h:mm:ss tt 8:30:22 PM
s Sortable DateTi­me  {0:s} yyyy'-'MM'-'dd'T'HH':'mm':'ss 2020-06-12T20:30:22
u Universal sorta­ble DateTime  {0:u} yyyy'-'MM'-'dd HH':'mm':'ss'Z' 2020-06-12 20:30:22Z
dd Day {0:dd} dd 12
ddd Short day name {0:ddd} ddd Tue
dddd Full day name {0:dddd} dddd Tuesday
MM Month {0:MM} MM 06
MMM Short month name {0:MMM} MMM Jun
MMMM Full month name {0:MMMM} MMMM June
yy Year with 2 digit {0:yy} yy 20
yyyy Year with 4 digit {0:yyyy} yyyy 2020
ss Seconds {0:ss} ss 22
tt AM/PM {0:tt} tt PM
ZZ Timezone offset 2 digit {0:ZZ} ZZ +07
ZZZ Timezone offset full {0:ZZZ} ZZZ +07:00

ABOUT THE AUTHOR

TechieClues
TechieClues

I specialize in creating and sharing insightful content encompassing various programming languages and technologies. My expertise extends to Python, PHP, Java, ... For more detailed information, please check out the user profile

https://www.techieclues.com/profile/techieclues

Comments (0)

There are no comments. Be the first to comment!!!