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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008#
Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo))
Console.WriteLine(date1.ToString("d", _
CultureInfo.CreateSpecificCulture("en-US")))
Console.WriteLine(date1.ToString("d", _
CultureInfo.CreateSpecificCulture("en-NZ")))
Console.WriteLine(date1.ToString("d", _
CultureInfo.CreateSpecificCulture("de-DE")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008,4, 10);
Console.WriteLine(date1.ToString("d", DateTimeFormatInfo.InvariantInfo));
Console.WriteLine(date1.ToString("d",
CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine(date1.ToString("d",
CultureInfo.CreateSpecificCulture("en-NZ")));
Console.WriteLine(date1.ToString("d",
CultureInfo.CreateSpecificCulture("de-DE")));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008#
Console.WriteLine(date1.ToString("D", _
CultureInfo.CreateSpecificCulture("en-US")))
Console.WriteLine(date1.ToString("D", _
CultureInfo.CreateSpecificCulture("pt-BR")))
Console.WriteLine(date1.ToString("D", _
CultureInfo.CreateSpecificCulture("es-MX")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10);
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("pt-BR")));
Console.WriteLine(date1.ToString("D",
CultureInfo.CreateSpecificCulture("es-MX")));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("f", _
CultureInfo.CreateSpecificCulture("en-US")))
Console.WriteLine(date1.ToString("f", _
CultureInfo.CreateSpecificCulture("fr-FR")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("f",
CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine(date1.ToString("f",
CultureInfo.CreateSpecificCulture("fr-FR")));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("F", _
CultureInfo.CreateSpecificCulture("en-US")))
Console.WriteLine(date1.ToString("F", _
CultureInfo.CreateSpecificCulture("fr-FR")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("F",
CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine(date1.ToString("F",
CultureInfo.CreateSpecificCulture("fr-FR")));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("g", _
DateTimeFormatInfo.InvariantInfo))
Console.WriteLine(date1.ToString("g", _
CultureInfo.CreateSpecificCulture("en-us")))
Console.WriteLine(date1.ToString("g", _
CultureInfo.CreateSpecificCulture("fr-BE")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("g",
DateTimeFormatInfo.InvariantInfo));
Console.WriteLine(date1.ToString("g",
CultureInfo.CreateSpecificCulture("en-us")));
Console.WriteLine(date1.ToString("g",
CultureInfo.CreateSpecificCulture("fr-BE")));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("G", _
DateTimeFormatInfo.InvariantInfo))
Console.WriteLine(date1.ToString("G", _
CultureInfo.CreateSpecificCulture("en-us")))
Console.WriteLine(date1.ToString("G", _
CultureInfo.CreateSpecificCulture("nl-BE")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("G",
DateTimeFormatInfo.InvariantInfo));
Console.WriteLine(date1.ToString("G",
CultureInfo.CreateSpecificCulture("en-us")));
Console.WriteLine(date1.ToString("G",
CultureInfo.CreateSpecificCulture("nl-BE")));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("m", _
CultureInfo.CreateSpecificCulture("en-us")))
Console.WriteLine(date1.ToString("m", _
CultureInfo.CreateSpecificCulture("ms-MY")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("m",
CultureInfo.CreateSpecificCulture("en-us")));
Console.WriteLine(date1.ToString("m",
CultureInfo.CreateSpecificCulture("ms-MY")));
|
|
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 hypens, 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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Dim dateOffset As New DateTimeOffset(date1, TimeZoneInfo.Local.GetUtcOFfset(date1))
Console.WriteLine(date1.ToString("o"))
Console.WriteLine(dateOffset.ToString("o"))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
DateTimeOffset dateOffset = new DateTimeOffset(date1,
TimeZoneInfo.Local.GetUtcOffset(date1));
Console.WriteLine(date1.ToString("o"));
Console.WriteLine(dateOffset.ToString("o"));
|
|
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.
Visual Basic |
Copy Code |
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"))
Console.WriteLine(dateOffset.ToUniversalTime.ToString("r"))
|
C# |
Copy Code |
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"));
Console.WriteLine(dateOffset.ToUniversalTime().ToString("r"));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("s"))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("s"));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("t", _
CultureInfo.CreateSpecificCulture("en-us")))
Console.WriteLine(date1.ToString("t", _
CultureInfo.CreateSpecificCulture("es-ES")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("t",
CultureInfo.CreateSpecificCulture("en-us")));
Console.WriteLine(date1.ToString("t",
CultureInfo.CreateSpecificCulture("es-ES")));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("T", _
CultureInfo.CreateSpecificCulture("en-us")))
Console.WriteLine(date1.ToString("T", _
CultureInfo.CreateSpecificCulture("es-ES")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("T",
CultureInfo.CreateSpecificCulture("en-us")));
Console.WriteLine(date1.ToString("T",
CultureInfo.CreateSpecificCulture("es-ES")));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToUniversalTime.ToString("u"))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToUniversalTime().ToString("u"));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("U", CultureInfo.CreateSpecificCulture("en-US")))
Console.WriteLine(date1.ToString("U", CultureInfo.CreateSpecificCulture("sv-FI")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("U",
CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine(date1.ToString("U",
CultureInfo.CreateSpecificCulture("sv-FI")));
|
|
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.
Visual Basic |
Copy Code |
Dim date1 As Date = #4/10/2008 6:30AM#
Console.WriteLine(date1.ToString("Y", CultureInfo.CreateSpecificCulture("en-US")))
Console.WriteLine(date1.ToString("y", CultureInfo.CreateSpecificCulture("af-ZA")))
|
C# |
Copy Code |
DateTime date1 = new DateTime(2008, 4, 10, 6, 30, 0);
Console.WriteLine(date1.ToString("Y",
CultureInfo.CreateSpecificCulture("en-US")));
Console.WriteLine(date1.ToString("y",
CultureInfo.CreateSpecificCulture("af-ZA")));
|
|
Any other single character |
(Unknown specifier) |
Throws a runtime
FormatException
. |