DateTime Formatting : DateTime Format « Date Time « C# / CSharp Tutorial






Format Character       Format Pattern                               Associated Property/ Description

D                      MM/dd/yyyy                                   ShortDatePattern

D                      dddd, MMMM dd, yyyy                          LongDatePattern

F                      dddd, MMMM dd, yyyy HH:mm                    Full date and time 

F                      dddd, MMMM dd, yyyy HH:mm:ss                 FullDateTimePattern

G                      MM/dd/yyyy HH:mm                             General 

G                      MM/dd/yyyy HH:mm:ss                          General 

M, M                   MMMM dd                                      MonthDayPattern

r, R                   ddd, dd MMM yyyy HH':'mm':'ss 'GMT'          RFC1123Pattern

S                      yyyy-MM-dd HH:mm:ss                          SortableDateTimePattern using local time

T                      HH:mm                                        ShortTimePattern

T                      HH:mm:ss                                     LongTimePattern

U                      yyyy-MM-dd HH:mm:ss                          UniversalSortableDateTimePattern using universal time

U                      dddd, MMMM dd, yyyy HH:mm:ss                 UniversalSortableDateTimePattern

y, Y                   MMMM, yyyy                                   YearMonthPattern








13.3.DateTime Format
13.3.1.DateTime Formatting
13.3.2.Format time and date: {0:hh:mm tt}
13.3.3.Format time and date: 24 hour time is {0:HH:mm}
13.3.4.Format time and date: Date is {0:ddd MMM dd, yyyy}
13.3.5.Format time and date: Date is {0:gg}
13.3.6.Format time and date: Time with seconds{0:HH:mm:ss tt}
13.3.7.Format time and date: Use m for day of month{0:m}
13.3.8.Format time and date: use m for minutes: {0:%m}
13.3.9.Format time and date information.
13.3.10.Use the ToLongDateString() and ToShortDateString() methods to convert the date parts of a DateTime to long and short date strings
13.3.11.Use the ToLongTimeString() and ToShortTimeString() methods to convert the time parts of a DateTime to long and short time strings
13.3.12.Use the ToString() method to convert a DateTime to a string: MMMM dd, yyyy
13.3.13.Use the ToString() method to convert a DateTime to a string: d, D, f, F, g, G, m ,r, s, t,T, u, U, y
13.3.14.Pre-built date/time specifiers
13.3.15.Culture-insensitive DateTime format strings
13.3.16.Culture-sensitive DateTime format strings
13.3.17.Format DateTime for different CultureInfo
13.3.18.DateTime long date pattern
13.3.19.Format DateTime with %M
13.3.20.DateTime custime format: MMMM dd, yyyy (dddd)
13.3.21.Format DayOfWeek