Export (0) Print
Expand All

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 format strings for the invariant culture, as well as the result string that is produced when that format string is used to format a particular date.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      DateTime date = new DateTime(2014, 8, 28, 12, 28, 30);
      DateTimeFormatInfo invDTF = new DateTimeFormatInfo();
      String[] formats = invDTF.GetAllDateTimePatterns();

      Console.WriteLine("{0,-40} {1}\n", "Pattern", "Result String");
      foreach (var fmt in formats)
         Console.WriteLine("{0,-40} {1}", fmt, date.ToString(fmt));
   }
}
// The example displays the following output: 
//    Pattern                                  Result String 
// 
//    MM/dd/yyyy                               08/28/2014 
//    yyyy-MM-dd                               2014-08-28 
//    dddd, dd MMMM yyyy                       Thursday, 28 August 2014 
//    dddd, dd MMMM yyyy HH:mm                 Thursday, 28 August 2014 12:28 
//    dddd, dd MMMM yyyy hh:mm tt              Thursday, 28 August 2014 12:28 PM 
//    dddd, dd MMMM yyyy H:mm                  Thursday, 28 August 2014 12:28 
//    dddd, dd MMMM yyyy h:mm tt               Thursday, 28 August 2014 12:28 PM 
//    dddd, dd MMMM yyyy HH:mm:ss              Thursday, 28 August 2014 12:28:30 
//    MM/dd/yyyy HH:mm                         08/28/2014 12:28 
//    MM/dd/yyyy hh:mm tt                      08/28/2014 12:28 PM 
//    MM/dd/yyyy H:mm                          08/28/2014 12:28 
//    MM/dd/yyyy h:mm tt                       08/28/2014 12:28 PM 
//    yyyy-MM-dd HH:mm                         2014-08-28 12:28 
//    yyyy-MM-dd hh:mm tt                      2014-08-28 12:28 PM 
//    yyyy-MM-dd H:mm                          2014-08-28 12:28 
//    yyyy-MM-dd h:mm tt                       2014-08-28 12:28 PM 
//    MM/dd/yyyy HH:mm:ss                      08/28/2014 12:28:30 
//    yyyy-MM-dd HH:mm:ss                      2014-08-28 12:28:30 
//    MMMM dd                                  August 28 
//    MMMM dd                                  August 28 
//    yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK   2014-08-28T12:28:30.0000000 
//    yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK   2014-08-28T12:28:30.0000000 
//    ddd, dd MMM yyyy HH':'mm':'ss 'GMT'      Thu, 28 Aug 2014 12:28:30 GMT 
//    ddd, dd MMM yyyy HH':'mm':'ss 'GMT'      Thu, 28 Aug 2014 12:28:30 GMT 
//    yyyy'-'MM'-'dd'T'HH':'mm':'ss            2014-08-28T12:28:30 
//    HH:mm                                    12:28 
//    hh:mm tt                                 12:28 PM 
//    H:mm                                     12:28 
//    h:mm tt                                  12:28 PM 
//    HH:mm:ss                                 12:28:30 
//    yyyy'-'MM'-'dd HH':'mm':'ss'Z'           2014-08-28 12:28:30Z 
//    dddd, dd MMMM yyyy HH:mm:ss              Thursday, 28 August 2014 12:28:30 
//    yyyy MMMM                                2014 August 
//    yyyy MMMM                                2014 August

The example instantiates a DateTimeFormatInfo object that represents the invariant culture by calling the DateTimeFormatInfo constructor. It could also retrieve a DateTimeFormatInfo that represents the invariant culture from the InvariantInfo property.

.NET Framework

Supported in: 4.5, 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