Share via


Date format in C# with reference to DateTimeFormatInfo

Various dateformat in C# and the examples fro the same given below. * DateTimeFormatInfo property Pattern value.

t   ShortTimePattern    h:mm tt
d   ShortDatePattern    M/d/yyyy
T   LongTimePattern h:mm:ss tt
D   LongDatePattern dddd, MMMM dd, yyyy
f   (combination of D and t)    dddd, MMMM dd, yyyy h:mm tt
F   FullDateTimePattern dddd, MMMM dd, yyyy h:mm:ss tt
g   (combination of d and t)    M/d/yyyy h:mm tt
G   (combination of d and T)    M/d/yyyy h:mm:ss tt
m, M    MonthDayPattern MMMM dd
y, Y    YearMonthPattern    MMMM, yyyy
r, R    RFC1123Pattern  ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (*)
s   SortableDateTi­mePattern    yyyy'-'MM'-'dd'T'HH':'mm':'ss (*)
u   UniversalSorta­bleDateTimePat­tern  yyyy'-'MM'-'dd HH':'mm':'ss'Z' (*)

Following examples show usage of standard format specifiers in String.Format method and the outputs.

String.Format("{0:t}", dt);  // "4:05 PM"                         ShortTime
String.Format("{0:d}", dt);  // "3/9/2008"                        ShortDate
String.Format("{0:T}", dt);  // "4:05:07 PM"                      LongTime
String.Format("{0:D}", dt);  // "Sunday, March 09, 2008"          LongDate
String.Format("{0:f}", dt);  // "Sunday, March 09, 2008 4:05 PM"  LongDate+ShortTime
String.Format("{0:F}", dt);  // "Sunday, March 09, 2008 4:05:07 PM" FullDateTime
String.Format("{0:g}", dt);  // "3/9/2008 4:05 PM"                ShortDate+ShortTime
String.Format("{0:G}", dt);  // "3/9/2008 4:05:07 PM"             ShortDate+LongTime
String.Format("{0:m}", dt);  // "March 09"                        MonthDay
String.Format("{0:y}", dt);  // "March, 2008"                     YearMonth
String.Format("{0:r}", dt);  // "Sun, 09 Mar 2008 16:05:07 GMT"   RFC1123
String.Format("{0:s}", dt);  // "2008-03-09T16:05:07"             SortableDateTime
String.Format("{0:u}", dt);  // "2008-03-09 16:05:07Z"            UniversalSortableDateTime

Original Post On LazzyCoder.com