Summary of C date formatting

  • 2020-05-19 05:37:19
  • OfStack

Date formatting

Standard DateTime format string

If the format strings contain only one of the individual format specifiers listed in the following table, they are interpreted as standard format specifiers. Throws an exception if the specified format character is a single character and is not included in the following table. If the format string is longer than a single character in length (even if the extra characters are blank), the format string is interpreted as a custom format string. Note that the patterns generated by these format specifiers are influenced by the Settings in the zone options control panel. Computers with different cultures or different date and time Settings will display different patterns.

The time and date delimiters displayed by the format string are defined by the DateSeparator and TimeSeparator characters associated with the DateTimeFormat property of the current culture. However, if InvariantCulture is referenced by the "r", "s", and "u" specifiers, the characters associated with the DateSeparator and TimeSeparator characters do not change with the current culture. The following table describes standard format strings for formatting DateTime objects.

The d short date pattern displays the pattern defined by the DateTimeFormatInfo.ShortDatePattern property associated with the current thread or by the specified format provider.

The D long date pattern displays the pattern defined by the DateTimeFormatInfo.LongDatePattern property associated with the current thread or by the specified format provider.

The t short duration pattern displays the pattern defined by the DateTimeFormatInfo.ShortTimePattern property associated with the current thread or by the specified format provider.

The T long time pattern displays the pattern defined by the DateTimeFormatInfo.LongTimePattern property associated with the current thread or by the specified format provider.

f full date/time mode (short time) shows a combination of long date and short time modes, separated by Spaces.

The F full date/time pattern (long time) displays the pattern defined by the DateTimeFormatInfo.FullDateTimePattern property associated with the current thread or by the specified format provider.

The g regular date/time mode (short time) shows a combination of short date and short time modes, separated by Spaces.

G regular date/time mode (long time) shows a combination of short date and long time modes, separated by Spaces.

The M or m month/day schema displays the schema defined by the DateTimeFormatInfo.MonthDayPattern property associated with the current thread or by the specified format provider.

The R or r RFC1123 schema displays the schema defined by the DateTimeFormatInfo.RFC1123Pattern attribute associated with the current thread or by the specified format provider. This is the definition criteria, and the property is read-only; Therefore, it is always the same regardless of the culture used or the format provider provided. Property reference CultureInfo.InvariantCulture property and follow the custom mode "ddd, dd MMMM yyyy HH:mm:ss G\MT". Note that "M" in "GMT" requires an escape character, so it is not interpreted.

s sortable date/time mode; Conforms to ISO 8601 to display a schema defined by the DateTimeFormatInfo.SortableDateTimePattern property associated with the current thread or by the specified format provider. Property refers to the CultureInfo.InvariantCulture property in a format following the custom schema "yyyy-MM-ddTHH :mm:ss".

The common sortable date/time pattern for u displays the pattern defined by the DateTimeFormatInfo.UniversalSortableDateTimePattern property associated with the current thread or by the specified format provider. Because it is a defined standard and the properties are read-only, the schema is always the same regardless of the culture or format provider. The format follows the custom pattern "yyyy-MM-dd HH:mm:ssZ".

The common sortable date/time schema for U displays the schema defined by the TimeSeparator.FullDateTimePattern attribute associated with the current thread or by the specified format provider. Note that the time shown is the universal time, not the local time.

The Y or y monthly schema displays the schema defined by the DateTimeFormatInfo.YearMonthPattern property associated with the current thread or by the specified format provider

The following table shows a list of format descriptor examples that apply to any value of DateTime.Now that exposes the current date and time information. Different culture Settings are shown in the example to illustrate the impact of changing the current culture. This usually changes in the following ways: use the date/time control panel in Microsoft Windows, pass your own DateTimeFormatInfo objects as a format provider, or pass the CultureInfo object Settings to a different culture. Note that for the "r" and "s" formats, the culture change does not affect the output. This table is a quick guide to how standard date and time specifiers affect formatting. See the code examples section below to illustrate these specifiers.

The format specifier is the current culture output

den US4/10/2001
den NZ10/04/2001
dde - DE10. 04.2001
Den - USTuesday April 10, 2001
Ten - US3:51:24 PM
Tes - ES15:51:24
fen-USTuesday, April 10, 2001 3:51 PM
ffr-FR mardi 10 avril 2001 15:51
ren-US Tue, 10 Apr 2001 15:51:24 GMT
rzh-SG Tue, 10 Apr 2001 15:51:24 GMT
sen US2001-04-10 T15:51:24
spt - BR T15:2001-04-10 51:24
uen - US 2001-04-10 15:51:24 Z
usv - FI 2001-04-10 15:51:24 Z
m en-USApril 10
mms-MY 10 April
yen-US April, 2001
yaf-ZAApril 2001
Format specifier not recognized by L en-UZ; Throws a format exception.

Date conversion 2


DateTime dt = DateTime.Now;
Label1.Text = dt.ToString();//2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();//127756416859912816
Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005 years 11 month 5 day 
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
Label1.Text = dt.Year.ToString();//2005
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//309
Label5.Text = dt.Hour.ToString();//13
Label6.Text = dt.Millisecond.ToString();//441
Label7.Text = dt.Minute.ToString();//30
Label8.Text = dt.Month.ToString();//11
Label9.Text = dt.Second.ToString();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
Label11.Text = dt.Add(?).ToString();// The question mark for 1 A period of time 
Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//1474088234
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime
Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005 years 11 month 
Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005 years 11 month 5 day 
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();// week 6 2005 11 05
Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();// week 6 2005 years 11 month 5 day 
Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11 month 5 day 
Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005 years 11 month 5 day  14:06
Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
Label1.Text = string.Format("{0:d}",dt);//2005-11-5
Label2.Text = string.Format("{0:D}",dt);//2005 years 11 month 5 day 
Label3.Text = string.Format("{0:f}",dt);//2005 years 11 month 5 day  14:23
Label4.Text = string.Format("{0:F}",dt);//2005 years 11 month 5 day  14:23:23
Label5.Text = string.Format("{0:g}",dt);//2005-11-5 14:23
Label6.Text = string.Format("{0:G}",dt);//2005-11-5 14:23:23
Label7.Text = string.Format("{0:M}",dt);//11 month 5 day 
Label8.Text = string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text = string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text   string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005 years 11 month 5 day  6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005 years 11 month 
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt);  


Related articles: