Export (0) Print
Expand All

DateTimeFormatInfo.SetAllDateTimePatterns Method

Updated: October 2010

Sets the custom date and time format strings that correspond to a specified standard format string.

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

[ComVisibleAttribute(false)]
public void SetAllDateTimePatterns(
	string[] patterns,
	char format
)

Parameters

patterns
Type: System.String[]

An array of custom format strings.

format
Type: System.Char

The standard format string associated with the custom format strings specified in the patterns parameter.

ExceptionCondition
ArgumentException

patterns is null or a zero-length array.

-or-

format is not a valid standard format string or is a standard format string whose patterns cannot be set.

ArgumentNullException

patterns has an array element whose value is null.

InvalidOperationException

This DateTimeFormatInfo object is read-only.

The SetAllDateTimePatterns method defines the custom format strings that correspond to a particular standard date and time format string. If a call to a date and time formatting method includes the standard date and time format string specified by format, the method uses the first element in the patterns array to define the format of the resulting string.

Caution noteCaution

The Parse and TryParse methods do not fully iterate all strings in patterns when parsing the string representation of a date and time. If you require a date and time string to have particular formats in a parsing operation, you should pass the array of valid formats to the DateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles), DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles), DateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime), or DateTimeOffset.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset) method.

You can define custom format strings that correspond to the "d", "D", "t", "T", and "y" or "Y" standard date and time format strings. If the value of format is any other standard format string, the SetAllDateTimePatterns method throws an ArgumentException.

If your custom date and time format strings include date separators, you should explicitly specify a date separator instead of relying on the parsing or formatting method that replaces the "/" custom format specifier with a particular date separator. For example, to obtain the pattern MM-DD-yyyy, use the pattern "MM-DD-yyyy".

The following example instantiates a CultureInfo object that represents the "en-US" (English - United States) culture and uses it to parse an array of date and time strings using the "Y" standard format string. It then uses the SetAllDateTimePatterns method to associate a new custom format string with the "Y" standard format string, and then attempts to parse the array of date and time strings. Output from the example demonstrates that the new custom format string is used in both the parsing and formatting operations.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Use standard en-US culture.
      CultureInfo enUS = new CultureInfo("en-US");

      string[] values = { "December 2010", "December, 2010",  
                          "Dec-2010", "December-2010" }; 

      Console.WriteLine("Supported Y/y patterns for {0} culture:", enUS.Name);
      foreach (var pattern in enUS.DateTimeFormat.GetAllDateTimePatterns('Y'))
         Console.WriteLine("   " + pattern);

      Console.WriteLine();

      // Try to parse each date string using "Y" format specifier.
      foreach (var value in values) {
         try {
            DateTime dat = DateTime.ParseExact(value, "Y", enUS);
            Console.WriteLine(String.Format(enUS, "   Parsed {0} as {1:Y}", value, dat));
         }
         catch (FormatException) {
            Console.WriteLine("   Cannot parse {0}", value);
         }   
      }   
      Console.WriteLine();

      //Modify supported "Y" format.
      enUS.DateTimeFormat.SetAllDateTimePatterns( new string[] { "MMM-yyyy" } , 'Y');

      Console.WriteLine("New supported Y/y patterns for {0} culture:", enUS.Name);
      foreach (var pattern in enUS.DateTimeFormat.GetAllDateTimePatterns('Y'))
         Console.WriteLine("   " + pattern);

      Console.WriteLine();

      // Try to parse each date string using "Y" format specifier.
      foreach (var value in values) {
         try {
            DateTime dat = DateTime.ParseExact(value, "Y", enUS);
            Console.WriteLine(String.Format(enUS, "   Parsed {0} as {1:Y}", value, dat));
         }
         catch (FormatException) {
            Console.WriteLine("   Cannot parse {0}", value);
         }   
      }   
   }
}
// The example displays the following output: 
//       Supported Y/y patterns for en-US culture: 
//          MMMM, yyyy 
//        
//          Cannot parse December 2010 
//          Parsed December, 2010 as December, 2010 
//          Cannot parse Dec-2010 
//          Cannot parse December-2010 
//        
//       New supported Y/y patterns for en-US culture: 
//          MMM-yyyy 
//        
//          Cannot parse December 2010 
//          Cannot parse December, 2010 
//          Parsed Dec-2010 as Dec-2010 
//          Cannot parse December-2010

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.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.

Date

History

Reason

October 2010

Noted that the array of custom formats is not fully iterated in parsing operations.

Content bug fix.

June 2010

Revised extensively, replaced the example, and added exception information.

Customer feedback.

Show:
© 2014 Microsoft