Permalink
Fetching contributors…
Cannot retrieve contributors at this time
417 lines (291 sloc) 45 KB
title ms.date ms.technology dev_langs helpviewer_keywords ms.assetid author ms.author
Standard Date and Time Format Strings
03/30/2017
dotnet-standard
csharp
vb
formatting [.NET Framework], dates
custom DateTime format string
format specifiers, custom date and time
format strings
custom date and time format strings
formatting [.NET Framework], time
date and time strings
bb79761a-ca08-44ee-b142-b06b3e2fc22b
rpetrusha
ronpet

Standard Date and Time Format Strings

A standard date and time format string uses a single format specifier to define the text representation of a date and time value. Any date and time format string that contains more than one character, including white space, is interpreted as a custom date and time format string; for more information, see Custom Date and Time Format Strings. A standard or custom format string can be used in two ways:

  • To define the string that results from a formatting operation.

  • To define the text representation of a date and time value that can be converted to a xref:System.DateTime or xref:System.DateTimeOffset value by a parsing operation.

[!TIP] You can download the Formatting Utility, an application that enables you to apply format strings to either numeric or date and time values and displays the result string.

Standard date and time format strings can be used with both xref:System.DateTime and xref:System.DateTimeOffset values.

[!INCLUDEC# interactive-note]

The following table describes the standard date and time format specifiers. Unless otherwise noted, a particular standard date and time format specifier produces an identical string representation regardless of whether it is used with a xref:System.DateTime or a xref:System.DateTimeOffset value. See the Notes section for additional information about using standard date and time format strings.

Format specifier Description Examples
"d" Short date pattern.

More information:The Short Date ("d") Format Specifier.
2009-06-15T13:45:30 -> 6/15/2009 (en-US)

2009-06-15T13:45:30 -> 15/06/2009 (fr-FR)

2009-06-15T13:45:30 -> 2009/06/15 (ja-JP)
"D" Long date pattern.

More information:The Long Date ("D") Format Specifier.
2009-06-15T13:45:30 -> Monday, June 15, 2009 (en-US)

2009-06-15T13:45:30 -> 15 июня 2009 г. (ru-RU)

2009-06-15T13:45:30 -> Montag, 15. Juni 2009 (de-DE)
"f" Full date/time pattern (short time).

More information: The Full Date Short Time ("f") Format Specifier.
2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45 PM (en-US)

2009-06-15T13:45:30 -> den 15 juni 2009 13:45 (sv-SE)

2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 1:45 μμ (el-GR)
"F" Full date/time pattern (long time).

More information: The Full Date Long Time ("F") Format Specifier.
2009-06-15T13:45:30 -> Monday, June 15, 2009 1:45:30 PM (en-US)

2009-06-15T13:45:30 -> den 15 juni 2009 13:45:30 (sv-SE)

2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 1:45:30 μμ (el-GR)
"g" General date/time pattern (short time).

More information: The General Date Short Time ("g") Format Specifier.
2009-06-15T13:45:30 -> 6/15/2009 1:45 PM (en-US)

2009-06-15T13:45:30 -> 15/06/2009 13:45 (es-ES)

2009-06-15T13:45:30 -> 2009/6/15 13:45 (zh-CN)
"G" General date/time pattern (long time).

More information: The General Date Long Time ("G") Format Specifier.
2009-06-15T13:45:30 -> 6/15/2009 1:45:30 PM (en-US)

2009-06-15T13:45:30 -> 15/06/2009 13:45:30 (es-ES)

2009-06-15T13:45:30 -> 2009/6/15 13:45:30 (zh-CN)
"M", "m" Month/day pattern.

More information: The Month ("M", "m") Format Specifier.
2009-06-15T13:45:30 -> June 15 (en-US)

2009-06-15T13:45:30 -> 15. juni (da-DK)

2009-06-15T13:45:30 -> 15 Juni (id-ID)
"O", "o" Round-trip date/time pattern.

More information: The Round-trip ("O", "o") Format Specifier.
xref:System.DateTime values:

2009-06-15T13:45:30 (DateTimeKind.Local) --> 2009-06-15T13:45:30.0000000-07:00

2009-06-15T13:45:30 (DateTimeKind.Utc) --> 2009-06-15T13:45:30.0000000Z

2009-06-15T13:45:30 (DateTimeKind.Unspecified) --> 2009-06-15T13:45:30.0000000

xref:System.DateTimeOffset values:

2009-06-15T13:45:30-07:00 --> 2009-06-15T13:45:30.0000000-07:00
"R", "r" RFC1123 pattern.

More information: The RFC1123 ("R", "r") Format Specifier.
2009-06-15T13:45:30 -> Mon, 15 Jun 2009 20:45:30 GMT
"s" Sortable date/time pattern.

More information: The Sortable ("s") Format Specifier.
2009-06-15T13:45:30 (DateTimeKind.Local) -> 2009-06-15T13:45:30

2009-06-15T13:45:30 (DateTimeKind.Utc) -> 2009-06-15T13:45:30
"t" Short time pattern.

More information: The Short Time ("t") Format Specifier.
2009-06-15T13:45:30 -> 1:45 PM (en-US)

2009-06-15T13:45:30 -> 13:45 (hr-HR)

2009-06-15T13:45:30 -> 01:45 م (ar-EG)
"T" Long time pattern.

More information: The Long Time ("T") Format Specifier.
2009-06-15T13:45:30 -> 1:45:30 PM (en-US)

2009-06-15T13:45:30 -> 13:45:30 (hr-HR)

2009-06-15T13:45:30 -> 01:45:30 م (ar-EG)
"u" Universal sortable date/time pattern.

More information: The Universal Sortable ("u") Format Specifier.
With a xref:System.DateTime value: 2009-06-15T13:45:30 -> 2009-06-15 13:45:30Z

With a xref:System.DateTimeOffset value: 2009-06-15T13:45:30 -> 2009-06-15 20:45:30Z
"U" Universal full date/time pattern.

More information: The Universal Full ("U") Format Specifier.
2009-06-15T13:45:30 -> Monday, June 15, 2009 8:45:30 PM (en-US)

2009-06-15T13:45:30 -> den 15 juni 2009 20:45:30 (sv-SE)

2009-06-15T13:45:30 -> Δευτέρα, 15 Ιουνίου 2009 8:45:30 μμ (el-GR)
"Y", "y" Year month pattern.

More information: The Year Month ("Y") Format Specifier.
2009-06-15T13:45:30 -> June, 2009 (en-US)

2009-06-15T13:45:30 -> juni 2009 (da-DK)

2009-06-15T13:45:30 -> Juni 2009 (id-ID)
Any other single character Unknown specifier. Throws a run-time xref:System.FormatException.

How Standard Format Strings Work

In a formatting operation, a standard format string is simply an alias for a custom format string. The advantage of using an alias to refer to a custom format string is that, although the alias remains invariant, the custom format string itself can vary. This is important because the string representations of date and time values typically vary by culture. For example, the "d" standard format string indicates that a date and time value is to be displayed using a short date pattern. For the invariant culture, this pattern is "MM/dd/yyyy". For the fr-FR culture, it is "dd/MM/yyyy". For the ja-JP culture, it is "yyyy/MM/dd".

If a standard format string in a formatting operation maps to a particular culture's custom format string, your application can define the specific culture whose custom format strings are used in one of these ways:

[!NOTE] For information about customizing the patterns or strings used in formatting date and time values, see the xref:System.Globalization.NumberFormatInfo class topic.

In some cases, the standard format string serves as a convenient abbreviation for a longer custom format string that is invariant. Four standard format strings fall into this category: "O" (or "o"), "R" (or "r"), "s", and "u". These strings correspond to custom format strings defined by the invariant culture. They produce string representations of date and time values that are intended to be identical across cultures. The following table provides information on these four standard date and time format strings.

Standard format string Defined by DateTimeFormatInfo.InvariantInfo property Custom format string
"O" or "o" None yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzz
"R" or "r" xref:System.Globalization.DateTimeFormatInfo.RFC1123Pattern%2A ddd, dd MMM yyyy HH':'mm':'ss 'GMT'
"s" xref:System.Globalization.DateTimeFormatInfo.SortableDateTimePattern%2A yyyy'-'MM'-'dd'T'HH':'mm':'ss
"u" xref:System.Globalization.DateTimeFormatInfo.UniversalSortableDateTimePattern%2A yyyy'-'MM'-'dd HH':'mm':'ss'Z'

Standard format strings can also be used in parsing operations with the xref:System.DateTime.ParseExact%2A?displayProperty=nameWithType or xref:System.DateTimeOffset.ParseExact%2A?displayProperty=nameWithType methods, which require an input string to exactly conform to a particular pattern for the parse operation to succeed. Many standard format strings map to multiple custom format strings, so a date and time value can be represented in a variety of formats and the parse operation will still succeed. You can determine the custom format string or strings that correspond to a standard format string by calling the xref:System.Globalization.DateTimeFormatInfo.GetAllDateTimePatterns%28System.Char%29?displayProperty=nameWithType method. The following example displays the custom format strings that map to the "d" (short date pattern) standard format string.

[!code-csharp-interactiveFormatting.DateAndTime.Standard#17] [!code-vbFormatting.DateAndTime.Standard#17]

The following sections describe the standard format specifiers for xref:System.DateTime and xref:System.DateTimeOffset values.

The Short Date ("d") Format Specifier

The "d" standard format specifier represents a custom date and time format string that is defined by a specific culture's xref:System.Globalization.DateTimeFormatInfo.ShortDatePattern%2A?displayProperty=nameWithType property. For example, the custom format string that is returned by the xref:System.Globalization.DateTimeFormatInfo.ShortDatePattern%2A property of the invariant culture is "MM/dd/yyyy".

The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that control the formatting of the returned string.

Property Description
xref:System.Globalization.DateTimeFormatInfo.ShortDatePattern%2A Defines the overall format of the result string.
xref:System.Globalization.DateTimeFormatInfo.DateSeparator%2A Defines the string that separates the year, month, and day components of a date.

The following example uses the "d" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#1] [!code-vbFormatting.DateAndTime.Standard#1]

Back to table

The Long Date ("D") Format Specifier

The "D" standard format specifier represents a custom date and time format string that is defined by the current xref:System.Globalization.DateTimeFormatInfo.LongDatePattern%2A?displayProperty=nameWithType property. For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy".

The following table lists the properties of the xref:System.Globalization.DateTimeFormatInfo object that control the formatting of the returned string.

Property Description
xref:System.Globalization.DateTimeFormatInfo.LongDatePattern%2A Defines the overall format of the result string.
xref:System.Globalization.DateTimeFormatInfo.DayNames%2A Defines the localized day names that can appear in the result string.
xref:System.Globalization.DateTimeFormatInfo.MonthNames%2A Defines the localized month names that can appear in the result string.

The following example uses the "D" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#2] [!code-vbFormatting.DateAndTime.Standard#2]

Back to table

The Full Date Short Time ("f") Format Specifier

The "f" standard format specifier represents a combination of the long date ("D") and short time ("t") patterns, separated by a space.

The result string is affected by the formatting information of a specific xref:System.Globalization.DateTimeFormatInfo object. The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that may control the formatting of the returned string. The custom format specifier returned by the xref:System.Globalization.DateTimeFormatInfo.LongDatePattern%2A?displayProperty=nameWithType and xref:System.Globalization.DateTimeFormatInfo.ShortTimePattern%2A?displayProperty=nameWithType properties of some cultures may not make use of all properties.

Property Description
xref:System.Globalization.DateTimeFormatInfo.LongDatePattern%2A Defines the format of the date component of the result string.
xref:System.Globalization.DateTimeFormatInfo.ShortTimePattern%2A Defines the format of the time component of the result string.
xref:System.Globalization.DateTimeFormatInfo.DayNames%2A Defines the localized day names that can appear in the result string.
xref:System.Globalization.DateTimeFormatInfo.MonthNames%2A Defines the localized month names that can appear in the result string.
xref:System.Globalization.DateTimeFormatInfo.TimeSeparator%2A Defines the string that separates the hour, minute, and second components of a time.
xref:System.Globalization.DateTimeFormatInfo.AMDesignator%2A Defines the string that indicates times from midnight to before noon in a 12-hour clock.
xref:System.Globalization.DateTimeFormatInfo.PMDesignator%2A Defines the string that indicates times from noon to before midnight in a 12-hour clock.

The following example uses the "f" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#3] [!code-vbFormatting.DateAndTime.Standard#3]

Back to table

The Full Date Long Time ("F") Format Specifier

The "F" standard format specifier represents a custom date and time format string that is defined by the current xref:System.Globalization.DateTimeFormatInfo.FullDateTimePattern%2A?displayProperty=nameWithType property. For example, the custom format string for the invariant culture is "dddd, dd MMMM yyyy HH:mm:ss".

The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that may control the formatting of the returned string. The custom format specifier that is returned by the xref:System.Globalization.DateTimeFormatInfo.FullDateTimePattern%2A property of some cultures may not make use of all properties.

Property Description
xref:System.Globalization.DateTimeFormatInfo.FullDateTimePattern%2A Defines the overall format of the result string.
xref:System.Globalization.DateTimeFormatInfo.DayNames%2A Defines the localized day names that can appear in the result string.
xref:System.Globalization.DateTimeFormatInfo.MonthNames%2A Defines the localized month names that can appear in the result string.
xref:System.Globalization.DateTimeFormatInfo.TimeSeparator%2A Defines the string that separates the hour, minute, and second components of a time.
xref:System.Globalization.DateTimeFormatInfo.AMDesignator%2A Defines the string that indicates times from midnight to before noon in a 12-hour clock.
xref:System.Globalization.DateTimeFormatInfo.PMDesignator%2A Defines the string that indicates times from noon to before midnight in a 12-hour clock.

The following example uses the "F" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#4] [!code-vbFormatting.DateAndTime.Standard#4]

Back to table

The General Date Short Time ("g") Format Specifier

The "g" standard format specifier represents a combination of the short date ("d") and short time ("t") patterns, separated by a space.

The result string is affected by the formatting information of a specific xref:System.Globalization.DateTimeFormatInfo object. The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that may control the formatting of the returned string. The custom format specifier that is returned by the xref:System.Globalization.DateTimeFormatInfo.ShortDatePattern%2A?displayProperty=nameWithType and xref:System.Globalization.DateTimeFormatInfo.ShortTimePattern%2A?displayProperty=nameWithType properties of some cultures may not make use of all properties.

Property Description
xref:System.Globalization.DateTimeFormatInfo.ShortDatePattern%2A Defines the format of the date component of the result string.
xref:System.Globalization.DateTimeFormatInfo.ShortTimePattern%2A Defines the format of the time component of the result string.
xref:System.Globalization.DateTimeFormatInfo.DateSeparator%2A Defines the string that separates the year, month, and day components of a date.
xref:System.Globalization.DateTimeFormatInfo.TimeSeparator%2A Defines the string that separates the hour, minute, and second components of a time.
xref:System.Globalization.DateTimeFormatInfo.AMDesignator%2A Defines the string that indicates times from midnight to before noon in a 12-hour clock.
xref:System.Globalization.DateTimeFormatInfo.PMDesignator%2A Defines the string that indicates times from noon to before midnight in a 12-hour clock.

The following example uses the "g" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#5] [!code-vbFormatting.DateAndTime.Standard#5]

Back to table

The General Date Long Time ("G") Format Specifier

The "G" standard format specifier represents a combination of the short date ("d") and long time ("T") patterns, separated by a space.

The result string is affected by the formatting information of a specific xref:System.Globalization.DateTimeFormatInfo object. The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that may control the formatting of the returned string. The custom format specifier that is returned by the xref:System.Globalization.DateTimeFormatInfo.ShortDatePattern%2A?displayProperty=nameWithType and xref:System.Globalization.DateTimeFormatInfo.LongTimePattern%2A?displayProperty=nameWithType properties of some cultures may not make use of all properties.

Property Description
xref:System.Globalization.DateTimeFormatInfo.ShortDatePattern%2A Defines the format of the date component of the result string.
xref:System.Globalization.DateTimeFormatInfo.LongTimePattern%2A Defines the format of the time component of the result string.
xref:System.Globalization.DateTimeFormatInfo.DateSeparator%2A Defines the string that separates the year, month, and day components of a date.
xref:System.Globalization.DateTimeFormatInfo.TimeSeparator%2A Defines the string that separates the hour, minute, and second components of a time.
xref:System.Globalization.DateTimeFormatInfo.AMDesignator%2A Defines the string that indicates times from midnight to before noon in a 12-hour clock.
xref:System.Globalization.DateTimeFormatInfo.PMDesignator%2A Defines the string that indicates times from noon to before midnight in a 12-hour clock.

The following example uses the "G" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#6] [!code-vbFormatting.DateAndTime.Standard#6]

Back to table

The Month ("M", "m") Format Specifier

The "M" or "m" standard format specifier represents a custom date and time format string that is defined by the current xref:System.Globalization.DateTimeFormatInfo.MonthDayPattern%2A?displayProperty=nameWithType property. For example, the custom format string for the invariant culture is "MMMM dd".

The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that control the formatting of the returned string.

Property Description
xref:System.Globalization.DateTimeFormatInfo.MonthDayPattern%2A Defines the overall format of the result string.
xref:System.Globalization.DateTimeFormatInfo.MonthNames%2A Defines the localized month names that can appear in the result string.

The following example uses the "m" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#7] [!code-vbFormatting.DateAndTime.Standard#7]

Back to table

The Round-trip ("O", "o") Format Specifier

The "O" or "o" standard format specifier represents a custom date and time format string using a pattern that preserves time zone information and emits a result string that complies with ISO 8601. For xref:System.DateTime values, this format specifier is designed to preserve date and time values along with the xref:System.DateTime.Kind%2A?displayProperty=nameWithType property in text. The formatted string can be parsed back by using the xref:System.DateTime.Parse%28System.String%2CSystem.IFormatProvider%2CSystem.Globalization.DateTimeStyles%29?displayProperty=nameWithType or xref:System.DateTime.ParseExact%2A?displayProperty=nameWithType method if the styles parameter is set to xref:System.Globalization.DateTimeStyles.RoundtripKind?displayProperty=nameWithType.

The "O" or "o" standard format specifier corresponds to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string for xref:System.DateTime values and to the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffzzz" custom format string for xref:System.DateTimeOffset values. In this string, the pairs of single quotation marks 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 do not appear in the output string.

The "O" or "o" standard format specifier (and the "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'fffffffK" custom format string) takes advantage of the three ways that ISO 8601 represents time zone information to preserve the xref:System.DateTime.Kind%2A property of xref:System.DateTime values:

  • The time zone component of xref:System.DateTimeKind.Local?displayProperty=nameWithType date and time values is an offset from UTC (for example, +01:00, -07:00). All xref:System.DateTimeOffset values are also represented in this format.

  • The time zone component of xref:System.DateTimeKind.Utc?displayProperty=nameWithType date and time values uses "Z" (which stands for zero offset) to represent UTC.

  • xref:System.DateTimeKind.Unspecified?displayProperty=nameWithType date and time values have no time zone information.

Because the O" or "o" standard format specifier conforms to an international standard, the formatting or parsing operation that uses the specifier always uses the invariant culture and the Gregorian calendar.

Strings that are passed to the Parse, TryParse, ParseExact, and TryParseExact methods of xref:System.DateTime and xref:System.DateTimeOffset can be parsed by using the "O" or "o" format specifier if they are in one of these formats. In the case of xref:System.DateTime objects, the parsing overload that you call should also include a styles parameter with a value of xref:System.Globalization.DateTimeStyles.RoundtripKind?displayProperty=nameWithType. Note that if you call a parsing method with the custom format string that corresponds to the "O" or "o" format specifier, you won't get the same results as "O" or "o". This is because parsing methods that use a custom format string can't parse the string representation of date and time values that lack a time zone component or use "Z" to indicate UTC.

The following example uses the "o" format specifier to display a series of xref:System.DateTime values and a xref:System.DateTimeOffset value on a system in the U.S. Pacific Time zone.

[!code-csharp-interactiveFormatting.DateAndTime.Standard#8] [!code-vbFormatting.DateAndTime.Standard#8]

The following example uses the "o" format specifier to create a formatted string, and then restores the original date and time value by calling a date and time Parse method.

[!code-csharpFormatting.DateandTime.Standard#16] [!code-vbFormatting.DateandTime.Standard#16]

Back to table

The RFC1123 ("R", "r") Format Specifier

The "R" or "r" standard format specifier represents a custom date and time format string that is defined by the xref:System.Globalization.DateTimeFormatInfo.RFC1123Pattern%2A?displayProperty=nameWithType 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.

The result string is affected by the following properties of the xref:System.Globalization.DateTimeFormatInfo object returned by the xref:System.Globalization.DateTimeFormatInfo.InvariantInfo%2A?displayProperty=nameWithType property that represents the invariant culture.

Property Description
xref:System.Globalization.DateTimeFormatInfo.RFC1123Pattern%2A Defines the format of the result string.
xref:System.Globalization.DateTimeFormatInfo.AbbreviatedDayNames%2A Defines the abbreviated day names that can appear in the result string.
xref:System.Globalization.DateTimeFormatInfo.AbbreviatedMonthNames%2A Defines the abbreviated month names that can appear in the result string.

Although the RFC 1123 standard expresses a time as Coordinated Universal Time (UTC), the formatting operation does not modify the value of the xref:System.DateTime object that is being formatted. Therefore, you must convert the xref:System.DateTime value to UTC by calling the xref:System.DateTime.ToUniversalTime%2A?displayProperty=nameWithType method before you perform the formatting operation. In contrast, xref:System.DateTimeOffset values perform this conversion automatically; there is no need to call the xref:System.DateTimeOffset.ToUniversalTime%2A?displayProperty=nameWithType method before the formatting operation.

The following example uses the "r" format specifier to display a xref:System.DateTime and a xref:System.DateTimeOffset value on a system in the U.S. Pacific Time zone.

[!code-csharp-interactiveFormatting.DateAndTime.Standard#9] [!code-vbFormatting.DateAndTime.Standard#9]

Back to table

The Sortable ("s") Format Specifier

The "s" standard format specifier represents a custom date and time format string that is defined by the xref:System.Globalization.DateTimeFormatInfo.SortableDateTimePattern%2A?displayProperty=nameWithType property. The pattern reflects a defined standard (ISO 8601), 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".

The purpose of the "s" format specifier is to produce result strings that sort consistently in ascending or descending order based on date and time values. As a result, although the "s" standard format specifier represents a date and time value in a consistent format, the formatting operation does not modify the value of the date and time object that is being formatted to reflect its xref:System.DateTime.Kind%2A?displayProperty=nameWithType property or its xref:System.DateTimeOffset.Offset%2A?displayProperty=nameWithType value. For example, the result strings produced by formatting the date and time values 2014-11-15T18:32:17+00:00 and 2014-11-15T18:32:17+08:00 are identical.

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 xref:System.DateTime and a xref:System.DateTimeOffset value on a system in the U.S. Pacific Time zone.

[!code-csharp-interactiveFormatting.DateAndTime.Standard#10] [!code-vbFormatting.DateAndTime.Standard#10]

Back to table

The Short Time ("t") Format Specifier

The "t" standard format specifier represents a custom date and time format string that is defined by the current xref:System.Globalization.DateTimeFormatInfo.ShortTimePattern%2A?displayProperty=nameWithType property. For example, the custom format string for the invariant culture is "HH:mm".

The result string is affected by the formatting information of a specific xref:System.Globalization.DateTimeFormatInfo object. The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that may control the formatting of the returned string. The custom format specifier that is returned by the xref:System.Globalization.DateTimeFormatInfo.ShortTimePattern%2A?displayProperty=nameWithType property of some cultures may not make use of all properties.

Property Description
xref:System.Globalization.DateTimeFormatInfo.ShortTimePattern%2A Defines the format of the time component of the result string.
xref:System.Globalization.DateTimeFormatInfo.TimeSeparator%2A Defines the string that separates the hour, minute, and second components of a time.
xref:System.Globalization.DateTimeFormatInfo.AMDesignator%2A Defines the string that indicates times from midnight to before noon in a 12-hour clock.
xref:System.Globalization.DateTimeFormatInfo.PMDesignator%2A Defines the string that indicates times from noon to before midnight in a 12-hour clock.

The following example uses the "t" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#11] [!code-vbFormatting.DateAndTime.Standard#11]

Back to table

The Long Time ("T") Format Specifier

The "T" standard format specifier represents a custom date and time format string that is defined by a specific culture's xref:System.Globalization.DateTimeFormatInfo.LongTimePattern%2A?displayProperty=nameWithType property. For example, the custom format string for the invariant culture is "HH:mm:ss".

The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that may control the formatting of the returned string. The custom format specifier that is returned by the xref:System.Globalization.DateTimeFormatInfo.LongTimePattern%2A?displayProperty=nameWithType property of some cultures may not make use of all properties.

Property Description
xref:System.Globalization.DateTimeFormatInfo.LongTimePattern%2A Defines the format of the time component of the result string.
xref:System.Globalization.DateTimeFormatInfo.TimeSeparator%2A Defines the string that separates the hour, minute, and second components of a time.
xref:System.Globalization.DateTimeFormatInfo.AMDesignator%2A Defines the string that indicates times from midnight to before noon in a 12-hour clock.
xref:System.Globalization.DateTimeFormatInfo.PMDesignator%2A Defines the string that indicates times from noon to before midnight in a 12-hour clock.

The following example uses the "T" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#12] [!code-vbFormatting.DateAndTime.Standard#12]

Back to table

The Universal Sortable ("u") Format Specifier

The "u" standard format specifier represents a custom date and time format string that is defined by the xref:System.Globalization.DateTimeFormatInfo.UniversalSortableDateTimePattern%2A?displayProperty=nameWithType 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.

Although the result string should express a time as Coordinated Universal Time (UTC), no conversion of the original xref:System.DateTime value is performed during the formatting operation. Therefore, you must convert a xref:System.DateTime value to UTC by calling the xref:System.DateTime.ToUniversalTime%2A?displayProperty=nameWithType method before formatting it. In contrast, xref:System.DateTimeOffset values perform this conversion automatically; there is no need to call the xref:System.DateTimeOffset.ToUniversalTime%2A?displayProperty=nameWithType method before the formatting operation.

The following example uses the "u" format specifier to display a date and time value.

[!code-csharp-interactiveFormatting.DateAndTime.Standard#13] [!code-vbFormatting.DateAndTime.Standard#13]

Back to table

The Universal Full ("U") Format Specifier

The "U" standard format specifier represents a custom date and time format string that is defined by a specified culture's xref:System.Globalization.DateTimeFormatInfo.FullDateTimePattern%2A?displayProperty=nameWithType property. The pattern is the same as the "F" pattern. However, the xref:System.DateTime value is automatically converted to UTC before it is formatted.

The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that may control the formatting of the returned string. The custom format specifier that is returned by the xref:System.Globalization.DateTimeFormatInfo.FullDateTimePattern%2A property of some cultures may not make use of all properties.

Property Description
xref:System.Globalization.DateTimeFormatInfo.FullDateTimePattern%2A Defines the overall format of the result string.
xref:System.Globalization.DateTimeFormatInfo.DayNames%2A Defines the localized day names that can appear in the result string.
xref:System.Globalization.DateTimeFormatInfo.MonthNames%2A Defines the localized month names that can appear in the result string.
xref:System.Globalization.DateTimeFormatInfo.TimeSeparator%2A Defines the string that separates the hour, minute, and second components of a time.
xref:System.Globalization.DateTimeFormatInfo.AMDesignator%2A Defines the string that indicates times from midnight to before noon in a 12-hour clock.
xref:System.Globalization.DateTimeFormatInfo.PMDesignator%2A Defines the string that indicates times from noon to before midnight in a 12-hour clock.

The "U" format specifier is not supported by the xref:System.DateTimeOffset type and throws a xref:System.FormatException if it is used to format a xref:System.DateTimeOffset value.

The following example uses the "U" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#14] [!code-vbFormatting.DateAndTime.Standard#14]

Back to table

The Year Month ("Y", "y") Format Specifier

The "Y" or "y" standard format specifier represents a custom date and time format string that is defined by the xref:System.Globalization.DateTimeFormatInfo.YearMonthPattern%2A?displayProperty=nameWithType property of a specified culture. For example, the custom format string for the invariant culture is "yyyy MMMM".

The following table lists the xref:System.Globalization.DateTimeFormatInfo object properties that control the formatting of the returned string.

Property Description
xref:System.Globalization.DateTimeFormatInfo.YearMonthPattern%2A Defines the overall format of the result string.
xref:System.Globalization.DateTimeFormatInfo.MonthNames%2A Defines the localized month names that can appear in the result string.

The following example uses the "y" format specifier to display a date and time value.

[!code-csharpFormatting.DateAndTime.Standard#15] [!code-vbFormatting.DateAndTime.Standard#15]

Back to table

Notes

Control Panel Settings

The settings in the Regional and Language Options item in Control Panel influence the result string produced by a formatting operation. These settings are used to initialize the xref:System.Globalization.DateTimeFormatInfo object associated with the current thread culture, which provides values used to govern formatting. Computers that use different settings generate different result strings.

In addition, if you use the xref:System.Globalization.CultureInfo.%23ctor%28System.String%29?displayProperty=nameWithType constructor to instantiate a new xref:System.Globalization.CultureInfo object that represents the same culture as the current system culture, any customizations established by the Regional and Language Options item in Control Panel will be applied to the new xref:System.Globalization.CultureInfo object. You can use the xref:System.Globalization.CultureInfo.%23ctor%28System.String%2CSystem.Boolean%29?displayProperty=nameWithType constructor to create a xref:System.Globalization.CultureInfo object that does not reflect a system's customizations.

DateTimeFormatInfo Properties

Formatting is influenced by properties of the current xref:System.Globalization.DateTimeFormatInfo object, which is provided implicitly by the current thread culture or explicitly by the xref:System.IFormatProvider parameter of the method that invokes formatting. For the xref:System.IFormatProvider parameter, your application should specify a xref:System.Globalization.CultureInfo object, which represents a culture, or a xref:System.Globalization.DateTimeFormatInfo object, which represents a particular culture's date and time formatting conventions. Many of the standard date and time format specifiers are aliases for formatting patterns defined by properties of the current xref:System.Globalization.DateTimeFormatInfo object. Your application can change the result produced by some standard date and time format specifiers by changing the corresponding date and time format patterns of the corresponding xref:System.Globalization.DateTimeFormatInfo property.

See also