Datetime formatting in C#

DateTime represents date and time, and datatime value can be displayed a variety of ways.

C# DateTime formatting is handled with the help of format pacifiers:

 

DDate in long form
dDate in short form
FDate and Time in long form
fDatea and Time in short form
GDate in short form, Time in Long form
gDate in short form, Time in short form
M or mMonth and day
R or rDate and Time in standard format(GMT)
SSortable date and time pattern
TTime in long form
tTime in short form
UUniversal long date/time pattern
uUniversal sortable date/time pattern
Y or yMonth and year

 example:

DateTime dt = DateTime.Now;
MessageBox.Show("D format : " + dt.ToString("D") + "\n" +
                 "d format : " + dt.ToString("d") + "\n" +
                 "F format : " + dt.ToString("F") + "\n" +
                 "f format : " + dt.ToString("f") + "\n" +
                 "G format : " + dt.ToString("G") + "\n" +
                 "g format : " + dt.ToString("g") + "\n" +
                 "M format : " + dt.ToString("M") + "\n" +
                 "m format : " + dt.ToString("m") + "\n" +
                 "R format : " + dt.ToString("R"));

 Output:

we can see messagebox with this information:

format : Saturday, June 18, 2011

d format : 06/18/2011

F format : Saturday, June 18, 2011 2:14:40 PM

f format : Saturday, June 18, 2011 2:14 PM

G format : 06/18/2011 2:14:40 PM

g format : 06/18/2011 2:14 PM

M format : June 18

m format : June 18

R format : Sat, 18 Jun 2011 14:14:40 GMT