DateTimeFormatInfo::GetAllDateTimePatterns Method (Char)
Returns all the patterns in which date and time values can be formatted using the specified standard format string.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- format
- Type: System::Char
A standard format string.
Return Value
Type: array<System::String>An array containing the standard patterns in which date and time values can be formatted using the specified format string.
| Exception | Condition |
|---|---|
| ArgumentException | format is not a valid standard format string. |
This method returns an array containing the custom format strings that correspond to a particular standard format string. See Standard Date and Time Format Strings for a list of the standard format strings.
You can use the custom format strings in the array returned by the GetAllDateTimePatterns method in formatting operations. However, if you do, the string representation of a date and time value returned in that formatting operation cannot always be parsed successfully by the Parse and TryParse methods. Therefore, you cannot assume that the custom format strings returned by the GetAllDateTimePatterns method can be used to round-trip date and time values. The following example illustrates this problem. It retrieves a DateTimeFormatInfo object that contains formatting information for the Russia (Russian) culture. It calls the GetAllDateTimePatterns method for each standard format string, and then passes each custom format string in the returned array to the DateTime::ToString(String) method to create the string representation of a date and time. This example then attempts to parse this value by calling the DateTime::TryParse(String, DateTime%) method. As the output from the example shows, some of the custom format strings do not produce a date and time value that successfully round-trips.
To parse the string representation of a date and time that can be expressed in a number of predefined custom formats, call one of the following methods:
DateTime::ParseExact(String, array<String>, IFormatProvider, DateTimeStyles)
DateTimeOffset::ParseExact(String, array<String>, IFormatProvider, DateTimeStyles)
DateTime::TryParseExact(String, array<String>, IFormatProvider, DateTimeStyles, DateTime%)
The custom format strings in the array returned by the GetAllDateTimePatterns method depends on the current calendar of the culture for which the DateTimeFormatInfo object supplies formatting information. If the calendar changes, the array returned by this method also changes.
The following example displays the date and time patterns for the current calendar.
#using <System.dll> using namespace System; using namespace System::Globalization; public ref class SamplesDateTimeFormatInfo { public: static void Main() { // Creates a new DateTimeFormatinfo. DateTimeFormatInfo^ myDtfi = gcnew DateTimeFormatInfo; // Gets and prints all the patterns array<String^>^myPatternsArray = myDtfi->GetAllDateTimePatterns(); Console::WriteLine( "ALL the patterns:" ); PrintIndexAndValues( myPatternsArray ); // Gets and prints the pattern(s) associated with some of the format characters. myPatternsArray = myDtfi->GetAllDateTimePatterns( 'd' ); Console::WriteLine( "The patterns for 'd':" ); PrintIndexAndValues( myPatternsArray ); myPatternsArray = myDtfi->GetAllDateTimePatterns( 'D' ); Console::WriteLine( "The patterns for 'D':" ); PrintIndexAndValues( myPatternsArray ); myPatternsArray = myDtfi->GetAllDateTimePatterns( 'f' ); Console::WriteLine( "The patterns for 'f':" ); PrintIndexAndValues( myPatternsArray ); myPatternsArray = myDtfi->GetAllDateTimePatterns( 'F' ); Console::WriteLine( "The patterns for 'F':" ); PrintIndexAndValues( myPatternsArray ); myPatternsArray = myDtfi->GetAllDateTimePatterns( 'r' ); Console::WriteLine( "The patterns for 'r':" ); PrintIndexAndValues( myPatternsArray ); myPatternsArray = myDtfi->GetAllDateTimePatterns( 'R' ); Console::WriteLine( "The patterns for 'R':" ); PrintIndexAndValues( myPatternsArray ); } public: static void PrintIndexAndValues( array<String^>^myArray ) { int i = 0; for each ( String^ s in myArray ) Console::WriteLine( "\t[{0}]:\t{1}", i++, s ); Console::WriteLine(); } }; int main() { SamplesDateTimeFormatInfo::Main(); } /* This code produces the following output. ALL the patterns: [0]: MM/dd/yyyy [1]: dddd, dd MMMM yyyy [2]: dddd, dd MMMM yyyy HH:mm [3]: dddd, dd MMMM yyyy hh:mm tt [4]: dddd, dd MMMM yyyy H:mm [5]: dddd, dd MMMM yyyy h:mm tt [6]: dddd, dd MMMM yyyy HH:mm:ss [7]: MM/dd/yyyy HH:mm [8]: MM/dd/yyyy hh:mm tt [9]: MM/dd/yyyy H:mm [10]: MM/dd/yyyy h:mm tt [11]: MM/dd/yyyy HH:mm:ss [12]: MMMM dd [13]: MMMM dd [14]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT' [15]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT' [16]: yyyy'-'MM'-'dd'T'HH':'mm':'ss [17]: HH:mm [18]: hh:mm tt [19]: H:mm [20]: h:mm tt [21]: HH:mm:ss [22]: yyyy'-'MM'-'dd HH':'mm':'ss'Z' [23]: dddd, dd MMMM yyyy HH:mm:ss [24]: yyyy MMMM [25]: yyyy MMMM The patterns for 'd': [0]: MM/dd/yyyy The patterns for 'D': [0]: dddd, dd MMMM yyyy The patterns for 'f': [0]: dddd, dd MMMM yyyy HH:mm [1]: dddd, dd MMMM yyyy hh:mm tt [2]: dddd, dd MMMM yyyy H:mm [3]: dddd, dd MMMM yyyy h:mm tt The patterns for 'F': [0]: dddd, dd MMMM yyyy HH:mm:ss The patterns for 'r': [0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT' The patterns for 'R': [0]: ddd, dd MMM yyyy HH':'mm':'ss 'GMT' */
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.