DateTimeFormatInfo.SetAllDateTimePatterns(String[], Char) DateTimeFormatInfo.SetAllDateTimePatterns(String[], Char) DateTimeFormatInfo.SetAllDateTimePatterns(String[], Char) DateTimeFormatInfo.SetAllDateTimePatterns(String[], Char) Method

定義

指定された標準書式指定文字列に対応する、カスタムの日時書式指定文字列を設定します。Sets the custom date and time format strings that correspond to a specified standard format string.

public:
 void SetAllDateTimePatterns(cli::array <System::String ^> ^ patterns, char format);
[System.Runtime.InteropServices.ComVisible(false)]
public void SetAllDateTimePatterns (string[] patterns, char format);
member this.SetAllDateTimePatterns : string[] * char -> unit
Public Sub SetAllDateTimePatterns (patterns As String(), format As Char)

パラメーター

patterns
String[]

カスタム書式指定文字列の配列。An array of custom format strings.

format
Char Char Char Char

patterns パラメーターに指定されたカスタム書式指定文字列に関連付けられている標準書式指定文字列。The standard format string associated with the custom format strings specified in the patterns parameter.

例外

patternsnull か、または長さ 0 の配列です。patterns is null or a zero-length array.

または-or- format が有効な標準書式指定文字列でないか、パターンを設定できない標準書式指定文字列です。format is not a valid standard format string or is a standard format string whose patterns cannot be set.

patterns は、値が null である配列要素を含んでいます。patterns has an array element whose value is null.

次の例ではCultureInfo 、"en-us" (英語米国) カルチャを表すオブジェクトをインスタンス化し、それを使用して、"Y" 標準書式指定文字列を使用して、日付と時刻の文字列の配列を解析します。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. 次に、 SetAllDateTimePatternsメソッドを使用して、新しいカスタム書式指定文字列を "Y" 標準書式指定文字列に関連付け、日付と時刻の文字列の配列を解析しようとします。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
Imports System.Globalization

Module Example
   Public Sub Main()
      ' Use standard en-US culture.
      Dim enUS As New CultureInfo("en-US")
      
      Dim values() As String = { "December 2010", "December, 2010",  
                                 "Dec-2010", "December-2010" } 
      
      Console.WriteLine("Supported Y/y patterns for {0} culture:", enUS.Name)
      For Each pattern In enUS.DateTimeFormat.GetAllDateTimePatterns("Y"c)
         Console.WriteLine("   " + pattern)
      Next 
      Console.WriteLine()
      
      ' Try to parse each date string using "Y" format specifier.
      For Each value In values
         Try
            Dim dat As Date = Date.ParseExact(value, "Y", enUS)
            Console.WriteLine(String.Format(enUS, "   Parsed {0} as {1:Y}", value, dat))
         Catch e As FormatException
            Console.WriteLine("   Cannot parse {0}", value)
         End Try   
      Next   
      Console.WriteLine()
      
      'Modify supported "Y" format.
      enUS.DateTimeFormat.SetAllDateTimePatterns( { "MMM-yyyy" } , "Y"c)
      
      Console.WriteLine("New supported Y/y patterns for {0} culture:", enUS.Name)
      For Each pattern In enUS.DateTimeFormat.GetAllDateTimePatterns("Y"c)
         Console.WriteLine("   " + pattern)
      Next 
      Console.WriteLine()

      ' Try to parse each date string using "Y" format specifier.
      For Each value In values
         Try
            Dim dat As Date = Date.ParseExact(value, "Y", enUS)
            Console.WriteLine(String.Format(enUS, "   Parsed {0} as {1:Y}", value, dat))
         Catch e As FormatException
            Console.WriteLine("   Cannot parse {0}", value)
         End Try   
      Next   
   End Sub
End Module
' 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

注釈

メソッドSetAllDateTimePatternsは、特定の標準日時書式指定文字列に対応するカスタム書式指定文字列を定義します。The SetAllDateTimePatterns method defines the custom format strings that correspond to a particular standard date and time format string. 日付と時刻の書式指定メソッドの呼び出しに、によって指定された標準のformat日時書式指定文字列が含まれているpatterns場合、メソッドは配列の最初の要素を使用して、結果の文字列の書式を定義します。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.

警告

メソッドParseTryParseメソッドは、日付と時刻の文字列patterns形式を解析するときに、内のすべての文字列を完全には反復処理しません。The Parse and TryParse methods do not fully iterate all strings in patterns when parsing the string representation of a date and time. 解析操作で特定の形式の日付と時刻の文字列を使用する必要があるDateTime.ParseExact(String, String[], IFormatProvider, DateTimeStyles)場合はDateTime.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTime)、有効な書式の配列を、 DateTimeOffset.ParseExact(String, String[], IFormatProvider, DateTimeStyles)、、またDateTimeOffset.TryParseExact(String, String[], IFormatProvider, DateTimeStyles, DateTimeOffset)はメソッドに渡す必要があります。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.

標準の日時書式指定文字列 "d"、"D"、"t"、"T"、"y"、"Y" に対応するカスタム書式指定文字列を定義できます。You can define custom format strings that correspond to the "d", "D", "t", "T", and "y" or "Y" standard date and time format strings. の値が他formatの標準書式指定文字列の場合、 SetAllDateTimePatternsメソッドはをArgumentExceptionスローします。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. たとえば、MM-DD-yyyy パターンを取得するには、パターン "MM-DD-yyyy" を使用します。For example, to obtain the pattern MM-DD-yyyy, use the pattern "MM-DD-yyyy".

適用対象

こちらもご覧ください