Export (0) Print
Expand All

DateTimeFormatInfo.GetAllDateTimePatterns Method (Char)

Returns all the patterns in which date and time values can be formatted using the specified standard format string.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)

public string[] GetAllDateTimePatterns(
	char format
)

Parameters

format
Type: System.Char

A standard format string.

Return Value

Type: System.String[]
An array containing the standard patterns in which date and time values can be formatted using the specified format string.

ExceptionCondition
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(Char) 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.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      CultureInfo culture = CultureInfo.CreateSpecificCulture("ru-RU");
      char[] formats = { 'd', 'D', 'f', 'F', 'g', 'G', 'm', 'o', 
                           'r', 's', 't', 'T', 'u', 'U', 'y' };
      DateTime date1 = new DateTime(2011, 02, 01, 7, 30, 45, 0);
      DateTime date2;
      int total = 0;
      int noRoundTrip = 0;

      foreach (var fmt in formats) {
         total = 0; 
         noRoundTrip = 0;
         foreach (var pattern in culture.DateTimeFormat.GetAllDateTimePatterns(fmt)) {
            total++;
            if (! DateTime.TryParse(date1.ToString(pattern), out date2)) {
               noRoundTrip++;
               Console.WriteLine("Unable to parse {0:" + pattern + "} (format '{1}')", 
                                 date1, pattern);
            }             
         }
         if (noRoundTrip > 0)
            Console.WriteLine("{0}: Unable to round-trip {1} of {2} format strings.\n",
                              fmt, noRoundTrip, total);
         else
            Console.WriteLine("{0}: All custom format strings round trip.\n", fmt);
      }
   }
}
// The example displays the following output: 
//    d: All custom format strings round trip. 
//     
//    Unable to parse 1 February 2011 г. (format 'd MMMM yyyy 'г.'') 
//    Unable to parse 01 February 2011 г. (format 'dd MMMM yyyy 'г.'') 
//    D: Unable to round-trip 2 of 2 format strings. 
//     
//    Unable to parse 1 February 2011 г. 7:30 (format 'd MMMM yyyy 'г.' H:mm') 
//    Unable to parse 1 February 2011 г. 07:30 (format 'd MMMM yyyy 'г.' HH:mm') 
//    Unable to parse 01 February 2011 г. 7:30 (format 'dd MMMM yyyy 'г.' H:mm') 
//    Unable to parse 01 February 2011 г. 07:30 (format 'dd MMMM yyyy 'г.' HH:mm') 
//    f: Unable to round-trip 4 of 4 format strings. 
//     
//    Unable to parse 1 February 2011 г. 7:30:45 (format 'd MMMM yyyy 'г.' H:mm:ss') 
//    Unable to parse 1 February 2011 г. 07:30:45 (format 'd MMMM yyyy 'г.' HH:mm:ss') 
//    Unable to parse 01 February 2011 г. 7:30:45 (format 'dd MMMM yyyy 'г.' H:mm:ss') 
//    Unable to parse 01 February 2011 г. 07:30:45 (format 'dd MMMM yyyy 'г.' HH:mm:ss') 
//    F: Unable to round-trip 4 of 4 format strings. 
//     
//    g: All custom format strings round trip. 
//     
//    G: All custom format strings round trip. 
//     
//    m: All custom format strings round trip. 
//     
//    o: All custom format strings round trip. 
//     
//    r: All custom format strings round trip. 
//     
//    s: All custom format strings round trip. 
//     
//    t: All custom format strings round trip. 
//     
//    T: All custom format strings round trip. 
//     
//    u: All custom format strings round trip. 
//     
//    Unable to parse 1 February 2011 г. 7:30:45 (format 'd MMMM yyyy 'г.' H:mm:ss') 
//    Unable to parse 1 February 2011 г. 07:30:45 (format 'd MMMM yyyy 'г.' HH:mm:ss') 
//    Unable to parse 01 February 2011 г. 7:30:45 (format 'dd MMMM yyyy 'г.' H:mm:ss') 
//    Unable to parse 01 February 2011 г. 07:30:45 (format 'dd MMMM yyyy 'г.' HH:mm:ss') 
//    U: Unable to round-trip 4 of 4 format strings. 
//     
//    y: All custom format strings round trip.

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:

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;
 using System.Globalization;
 public class SamplesDateTimeFormatInfo  {

    public static void Main()  {

       // Creates a new DateTimeFormatinfo.
       DateTimeFormatInfo myDtfi = new DateTimeFormatInfo();

       // Gets and prints all the patterns.
       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( String[] myArray )  {
       int i = 0;
       foreach ( String s in myArray )
          Console.WriteLine( "\t[{0}]:\t{1}", i++, s );
       Console.WriteLine();
    }
 }


/* 
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'
*/

.NET Framework

Supported in: 4.6, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft