Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

DateTimeFormatInfo.GetAllDateTimePatterns Method

Returns all the standard patterns in which date and time values can be formatted.

Namespace:  System.Globalization
Assembly:  mscorlib (in mscorlib.dll)
public string[] GetAllDateTimePatterns()

Return Value

Type: System.String[]
An array that contains the standard patterns in which date and time values can be formatted.

This method returns an array that contains all custom format strings that correspond to standard format strings. 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 Italy (Italian) culture. It passes each custom format string in the array returned by the GetAllDateTimePatterns() method 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("it-IT");
      DateTime date1 = new DateTime(2011, 02, 01, 7, 30, 45, 0);
      DateTime date2;
      int total = 0;
      int noRoundTrip = 0;

      foreach (var fmt in culture.DateTimeFormat.GetAllDateTimePatterns()) {
         total += 1;
         if (! DateTime.TryParse(date1.ToString(fmt), out date2)) {
            noRoundTrip++;
            Console.WriteLine("Unable to parse {0:" + fmt + "} (format '{1}')", 
                              date1, fmt);
         }             
      }
      Console.WriteLine("\nUnable to round-trip {0} of {1} format strings.",
                        noRoundTrip, total);

   }
}
// The example displays the following output: 
//    Unable to parse Tuesday 1 February 2011 7.30 (format 'dddd d MMMM yyyy H.mm') 
//    Unable to parse Tuesday 1 February 2011 07.30 (format 'dddd d MMMM yyyy HH.mm') 
//    Unable to parse 1-Feb-11 7.30 (format 'd-MMM-yy H.mm') 
//    Unable to parse 1-Feb-11 07.30 (format 'd-MMM-yy HH.mm') 
//    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 Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss') 
//    Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss') 
//    Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss') 
//    Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss') 
//    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/02/2011 7.30 (format 'dd/MM/yyyy H.mm') 
//    Unable to parse 01/02/2011 07.30 (format 'dd/MM/yyyy HH.mm') 
//    Unable to parse 01/Feb/2011 7.30 (format 'dd/MMM/yyyy H.mm') 
//    Unable to parse 01/Feb/2011 07.30 (format 'dd/MMM/yyyy HH.mm') 
//    Unable to parse 01/02/11 7.30 (format 'dd/MM/yy H.mm') 
//    Unable to parse 01/02/11 07.30 (format 'dd/MM/yy HH.mm') 
//    Unable to parse 01.2.11 7.30 (format 'dd.M.yy H.mm') 
//    Unable to parse 01.2.11 07.30 (format 'dd.M.yy HH.mm') 
//    Unable to parse 1/2/11 7.30 (format 'd/M/yy H.mm') 
//    Unable to parse 1/2/11 07.30 (format 'd/M/yy HH.mm') 
//    Unable to parse 2011-02-01 7.30 (format 'yyyy-MM-dd H.mm') 
//    Unable to parse 2011-02-01 07.30 (format 'yyyy-MM-dd HH.mm') 
//    Unable to parse 01/02/2011 7.30.45 (format 'dd/MM/yyyy H.mm.ss') 
//    Unable to parse 01/02/2011 07.30.45 (format 'dd/MM/yyyy HH.mm.ss') 
//    Unable to parse 01/Feb/2011 7.30.45 (format 'dd/MMM/yyyy H.mm.ss') 
//    Unable to parse 01/Feb/2011 07.30.45 (format 'dd/MMM/yyyy HH.mm.ss') 
//    Unable to parse 01/02/11 7.30.45 (format 'dd/MM/yy H.mm.ss') 
//    Unable to parse 01/02/11 07.30.45 (format 'dd/MM/yy HH.mm.ss') 
//    Unable to parse 01.2.11 7.30.45 (format 'dd.M.yy H.mm.ss') 
//    Unable to parse 01.2.11 07.30.45 (format 'dd.M.yy HH.mm.ss') 
//    Unable to parse 1/2/11 7.30.45 (format 'd/M/yy H.mm.ss') 
//    Unable to parse 1/2/11 07.30.45 (format 'd/M/yy HH.mm.ss') 
//    Unable to parse 2011-02-01 7.30.45 (format 'yyyy-MM-dd H.mm.ss') 
//    Unable to parse 2011-02-01 07.30.45 (format 'yyyy-MM-dd HH.mm.ss') 
//    Unable to parse Tuesday 1 February 2011 7.30.45 (format 'dddd d MMMM yyyy H.mm.ss') 
//    Unable to parse Tuesday 1 February 2011 07.30.45 (format 'dddd d MMMM yyyy HH.mm.ss') 
//    Unable to parse 1-Feb-11 7.30.45 (format 'd-MMM-yy H.mm.ss') 
//    Unable to parse 1-Feb-11 07.30.45 (format 'd-MMM-yy HH.mm.ss') 
//    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 round-trip 42 of 98 format strings.

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.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.