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. An exception is raised 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 affected by settings in the Regional Options control panel. Computers with different regionalities or different date and time settings will display different modes.
The time and date separators 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 the standard format strings for formatting DateTime objects.
d Short date mode Displays the pattern defined by the attributes associated with the current thread or the pattern defined by the specified format provider.
D Long date mode Displays the pattern defined by the attributes associated with the current thread or the pattern defined by the specified format provider.
t Short-time mode Displays the pattern defined by the attributes associated with the current thread or the pattern defined by the specified format provider.
T Long-time mode Displays the pattern defined by the attributes associated with the current thread or the pattern defined by the specified format provider.
f Full date/time mode (short time) Displays a combination of long date and short time modes, separated by spaces.
F Full date/time mode (long time) Displays the pattern defined by the attribute associated with the current thread or the pattern defined by the specified format provider.
g General Date/Time Mode (Short Time) Displays a combination of short date and short time modes, separated by spaces.
G General Date/Time Mode (Long Time) Displays a combination of short date and long time modes, separated by spaces.
M or m Monthly Day Mode Displays a pattern defined by the attribute associated with the current thread or a pattern defined by the specified format provider.
R or r RFC1123 Mode Displays a pattern defined by the DateTimeFormatInfo.RFC1123Pattern attribute associated with the current thread or a pattern defined by the specified format provider. This is the standard defined, and the properties are read-only; therefore, it is always the same regardless of the culture used or the format provider provided. Attribute references attributes 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 Sorted date/time pattern; in accordance with ISO 8601 Displays a pattern defined by the attribute associated with the current thread or a pattern defined by the specified format provider. Attribute reference Attribute, the format follows the custom mode "yyyy-MM-ddTHH:mm:ss".
u General sortable date/time mode Displays a pattern defined by the attributes associated with the current thread or a pattern defined by the specified format provider. Because it is the standard for definition and the properties are read-only, the pattern is always the same regardless of culture or format provider. The format follows the custom mode "yyyy-MM-dd HH:mm:ssZ".
U Common sortable date/time mode Displays a pattern defined by the attributes associated with the current thread or a pattern defined by the specified format provider. Note that the time displayed is the general time, not the local time.
Y or y Year and Month Mode Displays a pattern defined by the attribute associated with the current thread or a pattern defined by the specified format provider
The following table shows a list of format specifier examples that apply to any value exposing the current date and time information. Different culture settings are given in the example to illustrate the impact of changing the current culture. This is usually changed in the following ways: Use the Date/Time control panel in Microsoft Windows, pass your own DateTimeFormatInfo object as a format provider, or pass the CultureInfo object settings to different cultures. Note that for the "r" and "s" formats, changing culture does not affect the output. This table is a quick guide to how standard date and time specifiers affect formatting. See the code example section of this table explaining these specifiers below.
Format specifier Current culture Output
d en-US 4/10/2001
d en-NZ 10/04/2001
d de-DE 10.04.2001
D en-US Tuesday, April 10, 2001
T en-US 3:51:24 PM
T es-ES 15:51:24
f en-US Tuesday, April 10, 2001 3:51 PM
f fr-FR mardi 10 avril 2001 15:51
r en-US Tue, 10 Apr 2001 15:51:24 GMT
r zh-SG Tue, 10 Apr 2001 15:51:24 GMT
s en-US2001-04-10T15:51:24
s pt-BR 2001-04-10T15:51:24
u en-US 2001-04-10 15:51:24Z
u sv-FI 2001-04-10 15:51:24Z
m en-USApril 10
mms-MY 10 April
yen-US April, 2001
yaf-ZAApril 2001
L en-UZ Unrecognized format specifier; throws a format exception.
Date conversion 2
DateTime dt = ;
= ();//2005-11-5 13:21:25
= ().ToString();//127756416859912816
= ().ToString();//127756704859912816
= ().ToString();//2005-11-5 21:21:25
= ().ToString();//November 5, 2005
= ().ToString();//13:21:25
= ().ToString();//38661.5565508218
= ().ToString();//2005-11-5
= ().ToString();//13:21
= ().ToString();//2005-11-5 5:21:25
= ();//2005
= ();//2005-11-5 0:00:00
= ();//Saturday
= ();//309
= ();//13
= ();//441
= ();//30
= ();//11
= ();//28
= ();//632667942284412864
= ();//13:30:28.4412864
= ();//2005-11-5 13:47:04
= (1).ToString();//2006-11-5 13:47:04
= (1.1).ToString();//2005-11-6 16:11:04
= (1.1).ToString();//2005-11-5 14:53:04
= (1.1).ToString();//2005-11-5 13:47:04
= (1).ToString();//2005-12-5 13:47:04
= (1.1).ToString();//2005-11-5 13:47:05
= (1.1).ToString();//2005-11-5 13:48:10
= (1000).ToString();//2005-11-5 13:47:04
= (dt).ToString();//0
= (?).ToString();//The question mark is a time period
= ("2005-11-6 16:11:04").ToString();//False
= (dt).ToString();//True
= ().ToString();//1474088234
= ().ToString();//
= ().ToString();//DateTime
= ('s')[0].ToString();//2005-11-05T14:06:25
= ('t')[0].ToString();//14:06
= ('y')[0].ToString();//November 2005
= ('D')[0].ToString();//November 5, 2005
= ('D')[1].ToString();//2005 11 05
= ('D')[2].ToString();//Saturday 2005 11 05
= ('D')[3].ToString();//Saturday November 5, 2005
= ('M')[0].ToString();//November 5
= ('f')[0].ToString();//November 5, 2005 14:06
= ('g')[0].ToString();//2005-11-5 14:06
= ('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT
= ("{0:d}",dt);//2005-11-5
= ("{0:D}",dt);//November 5, 2005
= ("{0:f}",dt);//November 5, 2005 14:23
= ("{0:F}",dt);//November 5, 2005 14:23:23
= ("{0:g}",dt);//2005-11-5 14:23
= ("{0:G}",dt);//2005-11-5 14:23:23
= ("{0:M}",dt);//November 5
= ("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
= ("{0:s}",dt);//2005-11-05T14:23:23
("{0:t}",dt);//14:23
= ("{0:T}",dt);//14:23:23
= ("{0:u}",dt);//2005-11-05 14:23:23Z
= ("{0:U}",dt);//November 5, 2005 6:23:23
= ("{0:Y}",dt);//November 2005
= ("{0}",dt);//2005-11-5 14:23:23
= ("{0:yyyyMMddHHmmssffff}",dt);