Format specifier
|
Name
|
Description
|
|---|
d
|
Short date pattern
|
Represents a custom date and time format string defined by the current ShortDatePattern property. For example, the custom format string returned by the ShortDatePattern property of the invariant culture is "MM/dd/yyyy".
The following example uses the d format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008#
Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo))
' Displays 04/10/2008
Console.WriteLine(date1.ToString("d", _
CultureInfo.CreateSpecificCulture("en-US")))
' Displays 4/10/2008
Console.WriteLine(date1.ToString("d", _
CultureInfo.CreateSpecificCulture("en-NZ")))
' Displays 10/04/2008
Console.WriteLine(date1.ToString("d", _
CultureInfo.CreateSpecificCulture("de-DE")))
' Displays 10.04.2008
|
| DateTime date1 = new DateTime(2008,4, 10);
Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008
Console.WriteLine(date1.ToString("d",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays 4/10/2008
Console.WriteLine(date1.ToString("d",
CultureInfo.CreateSpecificCulture("en-NZ")));
// Displays 10/04/2008
Console.WriteLine(date1.ToString("d",
CultureInfo.CreateSpecificCulture("de-DE")));
// Displays 10.04.2008
|
|
D
|
Long date pattern
|
Represents a custom date and time format string defined by the current LongDatePattern property. For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy".
The following example uses the D format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008#
Console.WriteLine(date1.ToString("D", _
CultureInfo.CreateSpecificCulture("en-US")))
' Displays Thursday, April 10, 2008
Console.WriteLine(date1.ToString("D", _
CultureInfo.CreateSpecificCulture("pt-BR")))
' Displays quinta-feira, 10 de abril de 2008
Console.WriteLine(date1.ToString("D", _
CultureInfo.CreateSpecificCulture("es-MX")))
' Displays jueves, 10 de abril de 2008
|
| DateTime date1 = new DateTime(2008, 4, 10);
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("pt-BR")));
// Displays quinta-feira, 10 de abril de 2008
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("es-MX")));
// Displays jueves, 10 de abril de 2008
|
|
f
|
Full date/time pattern (short time)
|
Represents a combination of the long date (D) and short time (t) patterns, separated by a space.
The following example uses the f format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("f", _
CultureInfo.CreateSpecificCulture("en-US")))
' Displays Thursday, April 10, 2008 6:30 AM
Console.WriteLine(date1.ToString("f", _
CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays jeudi 10 avril 2008 06:30
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("f",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008 6:30 AM
Console.WriteLine(date1.ToString("f",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays jeudi 10 avril 2008 06:30
|
|
F
|
Full date/time pattern (long time)
|
Represents a custom date and time format string defined by the current FullDateTimePattern property. For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy HH:mm:ss".
The following example uses the F format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("F", _
CultureInfo.CreateSpecificCulture("en-US")))
' Displays Thursday, April 10, 2008 6:30:00 AM
Console.WriteLine(date1.ToString("F", _
CultureInfo.CreateSpecificCulture("fr-FR")))
' Displays jeudi 10 avril 2008 06:30:00
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("F",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008 6:30:00 AM
Console.WriteLine(date1.ToString("F",
CultureInfo.CreateSpecificCulture("fr-FR")));
// Displays jeudi 10 avril 2008 06:30:00
|
|
g
|
General date/time pattern (short time)
|
Represents a combination of the short date (d) and short time (t) patterns, separated by a space. The following example uses the g format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("g", _
DateTimeFormatInfo.InvariantInfo))
' Displays 04/10/2008 06:30
Console.WriteLine(date1.ToString("g", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays 4/10/2008 6:30 AM
Console.WriteLine(date1.ToString("g", _
CultureInfo.CreateSpecificCulture("fr-BE")))
' Displays10/04/2008 6:30
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("g",
DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008 06:30
Console.WriteLine(date1.ToString("g",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 4/10/2008 6:30 AM
Console.WriteLine(date1.ToString("g",
CultureInfo.CreateSpecificCulture("fr-BE")));
// Displays10/04/2008 6:30
|
|
G
|
General date/time pattern (long time)
|
Represents a combination of the short date (d) and long time (T) patterns, separated by a space. The following example uses the G format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("G", _
DateTimeFormatInfo.InvariantInfo))
' Displays 04/10/2008 06:30:00
Console.WriteLine(date1.ToString("G", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays 4/10/2008 6:30:00 AM
Console.WriteLine(date1.ToString("G", _
CultureInfo.CreateSpecificCulture("nl-BE")))
' Displays 10/04/2008 6:30:00
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("G",
DateTimeFormatInfo.InvariantInfo));
// Displays 04/10/2008 06:30:00
Console.WriteLine(date1.ToString("G",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 4/10/2008 6:30:00 AM
Console.WriteLine(date1.ToString("G",
CultureInfo.CreateSpecificCulture("nl-BE")));
// Displays 10/04/2008 6:30:00
|
|
M, m
|
Month day pattern
|
Represents a custom date and time format string defined by the current MonthDayPattern property. For example, the custom format string for the invariant culture is "MMMM dd". The following example uses the G format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("m", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays April 10
Console.WriteLine(date1.ToString("m", _
CultureInfo.CreateSpecificCulture("ms-MY")))
' Displays 10 April
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("m",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays April 10
Console.WriteLine(date1.ToString("m",
CultureInfo.CreateSpecificCulture("ms-MY")));
// Displays 10 April
|
|
O, o
|
Round-trip date/time pattern
|
Represents a custom date and time format string using a pattern that preserves time zone information. For DateTime values, this format specifier is designed to preserve date and time values along with the Kind property in text. Then the formatted string can be parsed back using Parse or ParseExact with the correct Kind property value.
The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" for DateTime values and "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz" for DateTimeOffset values. In this string, the pairs of apostrophes that delimit individual characters, such as the hyphens, the colons, and the letter "T", indicate that the individual character is a literal that cannot be changed. The apostrophes themselves do not appear in the output string.
The pattern for this specifier reflects a defined standard (ISO 8601). Therefore, it is always the same regardless of the culture used or the format provider supplied. Strings that are passed to the Parse or ParseExact method must conform exactly to this custom format pattern, or a FormatException is thrown.
When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.
The following example uses the o format specifier to display a DateTime and a DateTimeOffset value on a system in the U.S. Pacific Standard Time zone.
| Dim date1 As Date = #4/10/2008 6:30AM#
Dim dateOffset As New DateTimeOffset(date1, TimeZoneInfo.Local.GetUtcOFfset(date1))
Console.WriteLine(date1.ToString("o"))
' Displays 2008-04-10T06:30:00.0000000
Console.WriteLine(dateOffset.ToString("o"))
' Displays 2008-04-10T06:30:00.0000000-07:00
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
DateTimeOffset dateOffset = new DateTimeOffset(date1,
TimeZoneInfo.Local.GetUtcOffset(date1));
Console.WriteLine(date1.ToString("o"));
// Displays 2008-04-10T06:30:00.0000000
Console.WriteLine(dateOffset.ToString("o"));
// Displays 2008-04-10T06:30:00.0000000-07:00
|
|
R, r
|
RFC1123 pattern
|
Represents a custom date and time format string defined by the DateTimeFormatInfo..::.RFC1123Pattern property. The pattern reflects a defined standard and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied. The custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'".
When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.
Formatting does not modify the value of the DateTime or DateTimeOffset object that is being formatted. Therefore, the application must convert the value to Coordinated Universal Time (UTC) before using this format pattern.
The following example uses the r format specifier to display a DateTime and a DateTimeOffset value on a system in the U.S. Pacific Standard Time zone.
| Dim date1 As Date = #4/10/2008 6:30AM#
Dim dateOffset As New DateTimeOffset(date1, TimeZoneInfo.Local.GetUtcOFfset(date1))
Console.WriteLine(date1.ToUniversalTime.ToString("r"))
' Displays Thu, 10 Apr 2008 13:30:00 GMT
Console.WriteLine(dateOffset.ToUniversalTime.ToString("r"))
' Displays Thu, 10 Apr 2008 13:30:00 GMT
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
DateTimeOffset dateOffset = new DateTimeOffset(date1,
TimeZoneInfo.Local.GetUtcOffset(date1));
Console.WriteLine(date1.ToUniversalTime().ToString("r"));
// Displays Thu, 10 Apr 2008 13:30:00 GMT
Console.WriteLine(dateOffset.ToUniversalTime().ToString("r"));
// Displays Thu, 10 Apr 2008 13:30:00 GMT
|
|
s
|
Sortable date/time pattern; conforms to ISO 8601
|
Represents a custom date and time format string defined by the DateTimeFormatInfo..::.SortableDateTimePattern property. The pattern reflects a defined standard and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied. The custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss".
When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.
The following example uses the s format specifier to display a DateTime and a DateTimeOffset value on a system in the U.S. Pacific Standard Time zone.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("s"))
' Displays 2008-04-10T06:30:00
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("s"));
// Displays 2008-04-10T06:30:00
|
|
t
|
Short time pattern
|
Represents a custom date and time format string defined by the current ShortTimePattern property. For example, the custom format string for the invariant culture is "HH:mm".
The following example uses the t format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("t", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays 6:30 AM
Console.WriteLine(date1.ToString("t", _
CultureInfo.CreateSpecificCulture("es-ES")))
' Displays 6:30
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("t",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 6:30 AM
Console.WriteLine(date1.ToString("t",
CultureInfo.CreateSpecificCulture("es-ES")));
// Displays 6:30
|
|
T
|
Long time pattern
|
Represents a custom date and time format string defined by the current LongTimePattern property. For example, the custom format string for the invariant culture is "HH:mm:ss".
The following example uses the T format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("T", _
CultureInfo.CreateSpecificCulture("en-us")))
' Displays 6:30:00 AM
Console.WriteLine(date1.ToString("T", _
CultureInfo.CreateSpecificCulture("es-ES")))
' Displays 6:30:00
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("T",
CultureInfo.CreateSpecificCulture("en-us")));
// Displays 6:30:00 AM
Console.WriteLine(date1.ToString("T",
CultureInfo.CreateSpecificCulture("es-ES")));
// Displays 6:30:00
|
|
u
|
Universal sortable date/time pattern
|
Represents a custom date and time format string defined by the DateTimeFormatInfo..::.UniversalSortableDateTimePattern property. The pattern reflects a defined standard and the property is read-only. Therefore, it is always the same, regardless of the culture used or the format provider supplied. The custom format string is "yyyy'-'MM'-'dd HH':'mm':'ss'Z'".
When this standard format specifier is used, the formatting or parsing operation always uses the invariant culture.
Formatting does not convert the time zone for the date and time object. Therefore, the application must convert a date and time to Coordinated Universal Time (UTC) before using this format specifier.
The following example uses the u format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToUniversalTime.ToString("u"))
' Displays 2008-04-10 13:30:00Z
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToUniversalTime().ToString("u"));
// Displays 2008-04-10 13:30:00Z
|
|
U
|
Universal full date/time pattern
|
Represents a custom date and time format string defined by the current FullDateTimePattern property.
The pattern is the same as the F pattern. However, formatting operates on the UTC that is equivalent to the DateTime value.
The U format specifier is not supported by the DateTimeOffset type and throws a FormatException if it is used to format a DateTimeOffset value.
The following example uses the T format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("U", CultureInfo.CreateSpecificCulture("en-US")))
' Displays Thursday, April 10, 2008 1:30:00 PM
Console.WriteLine(date1.ToString("U", CultureInfo.CreateSpecificCulture("sv-FI")))
' Displays den 10 april 2008 13:30:00
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("U",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays Thursday, April 10, 2008 1:30:00 PM
Console.WriteLine(date1.ToString("U",
CultureInfo.CreateSpecificCulture("sv-FI")));
// Displays den 10 april 2008 13:30:00
|
|
Y, y
|
Year month pattern
|
Represents a custom date and time format string defined by the current YearMonthPattern property. For example, the custom format string for the invariant culture is "yyyy MMMM".
The following example uses the y format specifier to display a date and time value.
| Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("Y", CultureInfo.CreateSpecificCulture("en-US")))
' Displays April, 2008
Console.WriteLine(date1.ToString("y", CultureInfo.CreateSpecificCulture("af-ZA")))
' Displays April 2008
|
| DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("Y",
CultureInfo.CreateSpecificCulture("en-US")));
// Displays April, 2008
Console.WriteLine(date1.ToString("y",
CultureInfo.CreateSpecificCulture("af-ZA")));
// Displays April 2008
|
|
Any other single character
|
(Unknown specifier)
|
Throws a runtime FormatException.
|