DateTime.GetDateTimeFormats 方法

定義

將這個執行個體的值轉換為標準日期和時間格式規範所支援的所有字串表示。

多載

GetDateTimeFormats()

將這個執行個體的值轉換為標準日期和時間格式規範所支援的所有字串表示。

GetDateTimeFormats(Char)

將這個執行個體的值轉換為指定的標準日期和時間格式規範所支援的所有字串表示。

GetDateTimeFormats(IFormatProvider)

將這個執行個體的值轉換為標準日期和時間格式規範和指定的特定文化特性格式資訊所支援的所有字串表示。

GetDateTimeFormats(Char, IFormatProvider)

將這個執行個體的值轉換為指定的標準日期和時間格式規範和特定文化特性格式資訊所支援的所有字串表示。

備註

重要

您不應該假設多載的多個呼叫 GetDateTimeFormats 會傳回相同的資料。 根據特定的多載,這個方法所傳回的資料可能會在目前的文化特性變更、使用者覆寫個別的文化設定,或對系統的文化特性資料進行更新時變更。

GetDateTimeFormats()

將這個執行個體的值轉換為標準日期和時間格式規範所支援的所有字串表示。

public:
 cli::array <System::String ^> ^ GetDateTimeFormats();
public string[] GetDateTimeFormats ();
member this.GetDateTimeFormats : unit -> string[]
Public Function GetDateTimeFormats () As String()

傳回

String[]

字串陣列,其中的每個元素都是這個執行個體的值使用其中一個標準日期和時間格式規範所格式化的表示。

範例

下列範例會示範 DateTime.GetDateTimeFormats() 方法。 它會使用電腦目前文化特性中的所有可能標準日期和時間格式來顯示日期的字串表示,在此案例中為 en-us。

DateTime july28 = DateTime(2009, 7, 28, 5, 23, 15, 16);
array<String^>^july28Formats = july28.GetDateTimeFormats();

// Print [Out] july28* in all DateTime formats using the default culture.
System::Collections::IEnumerator^ myEnum = july28Formats->GetEnumerator();
while ( myEnum->MoveNext() )
{
   String^ format = safe_cast<String^>(myEnum->Current);
   System::Console::WriteLine( format );
}
DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15, 16);

string[] july28Formats = july28.GetDateTimeFormats();

// Print out july28 in all DateTime formats using the default culture.
foreach (string format in july28Formats) {
    Console.WriteLine(format);
}
Dim july28 As New DateTime(2009, 7, 28, 5, 23, 15, 16)
Dim july28Formats As String()
july28Formats = july28.GetDateTimeFormats()

' Print out july28 in all DateTime formats using the default culture.
For Each format As String In july28Formats
   Console.WriteLine(format)
Next

此範例會顯示下列輸出:

7/28/2009
7/28/09
07/28/09
07/28/2009
09/07/28
2009-07-28
28-Jul-09
Tuesday, July 28, 2009
July 28, 2009
Tuesday, 28 July, 2009
28 July, 2009
Tuesday, July 28, 2009 5:23 AM
Tuesday, July 28, 2009 05:23 AM
Tuesday, July 28, 2009 5:23
Tuesday, July 28, 2009 05:23
July 28, 2009 5:23 AM
July 28, 2009 05:23 AM
July 28, 2009 5:23
July 28, 2009 05:23
Tuesday, 28 July, 2009 5:23 AM
Tuesday, 28 July, 2009 05:23 AM
Tuesday, 28 July, 2009 5:23
Tuesday, 28 July, 2009 05:23
28 July, 2009 5:23 AM
28 July, 2009 05:23 AM
28 July, 2009 5:23
28 July, 2009 05:23
Tuesday, July 28, 2009 5:23:15 AM
Tuesday, July 28, 2009 05:23:15 AM
Tuesday, July 28, 2009 5:23:15
Tuesday, July 28, 2009 05:23:15
July 28, 2009 5:23:15 AM
July 28, 2009 05:23:15 AM
July 28, 2009 5:23:15
July 28, 2009 05:23:15
Tuesday, 28 July, 2009 5:23:15 AM
Tuesday, 28 July, 2009 05:23:15 AM
Tuesday, 28 July, 2009 5:23:15
Tuesday, 28 July, 2009 05:23:15
28 July, 2009 5:23:15 AM
28 July, 2009 05:23:15 AM
28 July, 2009 5:23:15
28 July, 2009 05:23:15
7/28/2009 5:23 AM
7/28/2009 05:23 AM
7/28/2009 5:23
7/28/2009 05:23
7/28/09 5:23 AM
7/28/09 05:23 AM
7/28/09 5:23
7/28/09 05:23
07/28/09 5:23 AM
07/28/09 05:23 AM
07/28/09 5:23
07/28/09 05:23
07/28/2009 5:23 AM
07/28/2009 05:23 AM
07/28/2009 5:23
07/28/2009 05:23
09/07/28 5:23 AM
09/07/28 05:23 AM
09/07/28 5:23
09/07/28 05:23
2009-07-28 5:23 AM
2009-07-28 05:23 AM
2009-07-28 5:23
2009-07-28 05:23
28-Jul-09 5:23 AM
28-Jul-09 05:23 AM
28-Jul-09 5:23
28-Jul-09 05:23
7/28/2009 5:23:15 AM
7/28/2009 05:23:15 AM
7/28/2009 5:23:15
7/28/2009 05:23:15
7/28/09 5:23:15 AM
7/28/09 05:23:15 AM
7/28/09 5:23:15
7/28/09 05:23:15
07/28/09 5:23:15 AM
07/28/09 05:23:15 AM
07/28/09 5:23:15
07/28/09 05:23:15
07/28/2009 5:23:15 AM
07/28/2009 05:23:15 AM
07/28/2009 5:23:15
07/28/2009 05:23:15
09/07/28 5:23:15 AM
09/07/28 05:23:15 AM
09/07/28 5:23:15
09/07/28 05:23:15
2009-07-28 5:23:15 AM
2009-07-28 05:23:15 AM
2009-07-28 5:23:15
2009-07-28 05:23:15
28-Jul-09 5:23:15 AM
28-Jul-09 05:23:15 AM
28-Jul-09 5:23:15
28-Jul-09 05:23:15
July 28
July 28
2009-07-28T05:23:15.0160000
2009-07-28T05:23:15.0160000
Tue, 28 Jul 2009 05:23:15 GMT
Tue, 28 Jul 2009 05:23:15 GMT
2009-07-28T05:23:15
5:23 AM
05:23 AM
5:23
05:23
5:23:15 AM
05:23:15 AM
5:23:15
05:23:15
2009-07-28 05:23:15Z
Tuesday, July 28, 2009 12:23:15 PM
Tuesday, July 28, 2009 12:23:15 PM
Tuesday, July 28, 2009 12:23:15
Tuesday, July 28, 2009 12:23:15
July 28, 2009 12:23:15 PM
July 28, 2009 12:23:15 PM
July 28, 2009 12:23:15
July 28, 2009 12:23:15
Tuesday, 28 July, 2009 12:23:15 PM
Tuesday, 28 July, 2009 12:23:15 PM
Tuesday, 28 July, 2009 12:23:15
Tuesday, 28 July, 2009 12:23:15
28 July, 2009 12:23:15 PM
28 July, 2009 12:23:15 PM
28 July, 2009 12:23:15
28 July, 2009 12:23:15
July, 2009
July, 2009

備註

方法所傳回的字串陣列 DateTime.GetDateTimeFormats() 相當於將個別呼叫方法所傳回的字串陣列 DateTime.GetDateTimeFormats(Char) 與 "d"、"d"、"f"、"f"、"g"、"g"、"m"、"o"、"r"、"s"、"t"、"t"、"u"、"u" 和 "y" 標準格式字串。 如需標準格式規範的詳細資訊,請參閱 標準日期和時間格式字串

傳回值的每個專案都會使用目前文化特性的資訊進行格式化。 如需目前文化特性的特定文化特性格式資訊的詳細資訊,請參閱 CultureInfo.CurrentCulture

重要

因為此方法會使用區分文化特性的資料,所以您不應該假設對方法的多個呼叫會傳回相同的資料。 如果目前的文化特性變更、使用者覆寫個別的文化設定,或對系統的文化特性資料進行更新,這個方法所傳回的資料可能會變更。

另請參閱

適用於

GetDateTimeFormats(Char)

將這個執行個體的值轉換為指定的標準日期和時間格式規範所支援的所有字串表示。

public:
 cli::array <System::String ^> ^ GetDateTimeFormats(char format);
public string[] GetDateTimeFormats (char format);
member this.GetDateTimeFormats : char -> string[]
Public Function GetDateTimeFormats (format As Char) As String()

參數

format
Char

標準日期和時間格式字串。

傳回

String[]

字串陣列,其中的每個項目都是這個執行個體的值使用 format 標準日期和時間格式規範所格式化的表示。

例外狀況

format 不是有效的標準日期和時間格式規範字元。

範例

下列範例會示範 GetDateTimeFormats(Char) 方法。 它會使用目前文化特性的完整日期格式規範 ( "D" ) 來顯示日期的字串表示。

  DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
  
  // Get the long date formats using the current culture.
  string [] longJuly28Formats =
              july28.GetDateTimeFormats('D');

  // Display july28 in all long date formats.
  foreach (string format in longJuly28Formats) {
      Console.WriteLine(format);
  }
// The example displays the following output:
//       Tuesday, July 28, 2009
//       July 28, 2009
//       Tuesday, 28 July, 2009
//       28 July, 2009
  Dim july28 As Date = #7/28/2009 5:23:15#
  
  ' Get the long date formats using the current culture.
  Dim longJuly28Formats() As String = july28.GetDateTimeFormats("D"c)

  ' Display july28 in all long date formats.
  For Each format As String In longJuly28Formats
      Console.WriteLine(format)
Next			
' The example displays the following output:
'       Tuesday, July 28, 2009
'       July 28, 2009
'       Tuesday, 28 July, 2009
'       28 July, 2009

備註

format參數可以是任何標準日期和時間格式規範。 這些包括 d、D、f、F、g、G、M (或 M) 、O (或 o) 、R (或 R) 、s、t、T、u、U、Y (或 y) 。 如需詳細資訊,請參閱 標準日期和時間格式字串

傳回值的每個專案都會使用目前文化特性的資訊進行格式化。 如需目前文化特性的特定文化特性格式資訊的詳細資訊,請參閱 CultureInfo.CurrentCulture

重要

因為此方法會使用區分文化特性的資料,所以您不應該假設對方法的多個呼叫會傳回相同的資料。 如果目前的文化特性變更、使用者覆寫個別的文化設定,或對系統的文化特性資料進行更新,這個方法所傳回的資料可能會變更。

另請參閱

適用於

GetDateTimeFormats(IFormatProvider)

將這個執行個體的值轉換為標準日期和時間格式規範和指定的特定文化特性格式資訊所支援的所有字串表示。

public:
 cli::array <System::String ^> ^ GetDateTimeFormats(IFormatProvider ^ provider);
public string[] GetDateTimeFormats (IFormatProvider provider);
public string[] GetDateTimeFormats (IFormatProvider? provider);
member this.GetDateTimeFormats : IFormatProvider -> string[]
Public Function GetDateTimeFormats (provider As IFormatProvider) As String()

參數

provider
IFormatProvider

物件,提供關於這個執行個體的文化特性格式資訊。

傳回

String[]

字串陣列,其中的每個元素都是這個執行個體的值使用其中一個標準日期和時間格式規範所格式化的表示。

範例

下列範例會示範 DateTime.GetDateTimeFormats(IFormatProvider) 方法。 它會使用 fr-fr 文化特性的所有可能標準日期和時間格式來顯示日期的字串表示。

DateTime juil28 = DateTime(2009, 7, 28, 5, 23, 15, 16);
IFormatProvider^ culture = gcnew System::Globalization::CultureInfo("fr-FR", true );

// Get the short date formats using the S"fr-FR" culture.
array<String^>^frenchJuly28Formats = juil28.GetDateTimeFormats(culture );

// Print [Out] july28* in all DateTime formats using fr-FR culture.
System::Collections::IEnumerator^ myEnum2 = frenchJuly28Formats->GetEnumerator();
while ( myEnum2->MoveNext() )
{
   String^ format = safe_cast<String^>(myEnum2->Current);
   System::Console::WriteLine(format );
}
DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15, 16);

IFormatProvider culture =
    new System.Globalization.CultureInfo("fr-FR", true);
// Get the short date formats using the "fr-FR" culture.
string [] frenchJuly28Formats =
            july28.GetDateTimeFormats(culture);

// Display july28 in various formats using "fr-FR" culture.
foreach (string format in frenchJuly28Formats) {
    Console.WriteLine(format);
}
Dim july28 As New DateTime(2009, 7, 28, 5, 23, 15, 16)
Dim culture As New System.Globalization.CultureInfo("fr-FR", True)

Dim july28Formats As String()
' Get the short date formats using the "fr-FR" culture.
july28Formats = july28.GetDateTimeFormats(culture)

' Print out july28 in various formats using "fr-FR" culture.
For Each format As String In july28Formats
   Console.WriteLine(format)
Next

此範例會顯示下列輸出:

28/07/2009
28/07/09
28.07.09
28-07-09
2009-07-28
mardi 28 juillet 2009
28 juil. 09
28 juillet 2009
mardi 28 juillet 2009 05:23
mardi 28 juillet 2009 5:23
mardi 28 juillet 2009 05.23
mardi 28 juillet 2009 05 h 23
28 juil. 09 05:23
28 juil. 09 5:23
28 juil. 09 05.23
28 juil. 09 05 h 23
28 juillet 2009 05:23
28 juillet 2009 5:23
28 juillet 2009 05.23
28 juillet 2009 05 h 23
mardi 28 juillet 2009 05:23:15
mardi 28 juillet 2009 5:23:15
mardi 28 juillet 2009 05.23
mardi 28 juillet 2009 05 h 23
28 juil. 09 05:23:15
28 juil. 09 5:23:15
28 juil. 09 05.23
28 juil. 09 05 h 23
28 juillet 2009 05:23:15
28 juillet 2009 5:23:15
28 juillet 2009 05.23
28 juillet 2009 05 h 23
28/07/2009 05:23
28/07/2009 5:23
28/07/2009 05.23
28/07/2009 05 h 23
28/07/09 05:23
28/07/09 5:23
28/07/09 05.23
28/07/09 05 h 23
28.07.09 05:23
28.07.09 5:23
28.07.09 05.23
28.07.09 05 h 23
28-07-09 05:23
28-07-09 5:23
28-07-09 05.23
28-07-09 05 h 23
2009-07-28 05:23
2009-07-28 5:23
2009-07-28 05.23
2009-07-28 05 h 23
28/07/2009 05:23:15
28/07/2009 5:23:15
28/07/2009 05.23
28/07/2009 05 h 23
28/07/09 05:23:15
28/07/09 5:23:15
28/07/09 05.23
28/07/09 05 h 23
28.07.09 05:23:15
28.07.09 5:23:15
28.07.09 05.23
28.07.09 05 h 23
28-07-09 05:23:15
28-07-09 5:23:15
28-07-09 05.23
28-07-09 05 h 23
2009-07-28 05:23:15
2009-07-28 5:23:15
2009-07-28 05.23
2009-07-28 05 h 23
28 juillet
28 juillet
2009-07-28T05:23:15.0160000
2009-07-28T05:23:15.0160000
Tue, 28 Jul 2009 05:23:15 GMT
Tue, 28 Jul 2009 05:23:15 GMT
2009-07-28T05:23:15
05:23
5:23
05.23
05 h 23
05:23:15
5:23:15
05.23
05 h 23
2009-07-28 05:23:15Z
mardi 28 juillet 2009 12:23:15
mardi 28 juillet 2009 12:23:15
mardi 28 juillet 2009 12.23
mardi 28 juillet 2009 12 h 23
28 juil. 09 12:23:15
28 juil. 09 12:23:15
28 juil. 09 12.23
28 juil. 09 12 h 23
28 juillet 2009 12:23:15
28 juillet 2009 12:23:15
28 juillet 2009 12.23
28 juillet 2009 12 h 23
juillet 2009
juillet 2009

備註

方法所傳回的字串陣列 DateTime.GetDateTimeFormats(IFormatProvider) 相當於將個別呼叫方法所傳回的字串陣列 DateTime.GetDateTimeFormats(Char, IFormatProvider) 與 "d"、"d"、"f"、"f"、"g"、"g"、"m"、"o"、"r"、"s"、"t"、"t"、"u"、"u" 和 "y" 標準格式字串。 如需標準格式規範的詳細資訊,請參閱 標準日期和時間格式字串

傳回值的每個專案都會使用所提供的特定文化特性資訊來格式化 provider

重要

因為此方法會使用區分文化特性的資料,所以您不應該假設對方法的多個呼叫會傳回相同的資料,除非是代表不因文化特性而異的 provider CultureInfo 物件。 如果使用者覆寫的個別文化設定, provider 或如果系統的文化特性資料發生更新,這個方法所傳回的資料可能會變更 provider

另請參閱

適用於

GetDateTimeFormats(Char, IFormatProvider)

將這個執行個體的值轉換為指定的標準日期和時間格式規範和特定文化特性格式資訊所支援的所有字串表示。

public:
 cli::array <System::String ^> ^ GetDateTimeFormats(char format, IFormatProvider ^ provider);
public string[] GetDateTimeFormats (char format, IFormatProvider provider);
public string[] GetDateTimeFormats (char format, IFormatProvider? provider);
member this.GetDateTimeFormats : char * IFormatProvider -> string[]
Public Function GetDateTimeFormats (format As Char, provider As IFormatProvider) As String()

參數

format
Char

日期和時間格式字串。

provider
IFormatProvider

物件,提供關於這個執行個體的文化特性格式資訊。

傳回

String[]

字串陣列,其中的每個元素都是這個執行個體的值使用其中一個標準日期和時間格式規範所格式化的表示。

例外狀況

format 不是有效的標準日期和時間格式規範字元。

範例

下列範例會示範 GetDateTimeFormats(Char, IFormatProvider) 方法。 它會針對 fr-fr 文化特性,使用簡短日期格式規範 ( "d" ) 來顯示日期的字串表示。

  DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15);
  
  IFormatProvider culture =
      new System.Globalization.CultureInfo("fr-FR", true);
  // Get the short date formats using the "fr-FR" culture.
  string [] frenchJuly28Formats =
              july28.GetDateTimeFormats('d', culture);

  // Display july28 in short date formats using "fr-FR" culture.
  foreach (string format in frenchJuly28Formats) {
      Console.WriteLine(format);
  }
// The example displays the following output:
//       28/07/2009
//       28/07/09
//       28.07.09
//       28-07-09
//       2009-07-28
  Dim july28 As Date = #7/28/2009 5:23:15#
  
  Dim culture As IFormatProvider = New System.Globalization.CultureInfo("fr-FR", True)
  ' Get the short date formats using the "fr-FR" culture.
  Dim frenchJuly28Formats() As String = july28.GetDateTimeFormats("d"c, culture)

  ' Display july28 in short date formats using "fr-FR" culture.
  For Each format As String In frenchJuly28Formats
      Console.WriteLine(format)
  Next
' The example displays the following output:
'       28/07/2009
'       28/07/09
'       28.07.09
'       28-07-09
'       2009-07-28

備註

Format 參數可以是任何標準日期和時間格式規範。 這些包括 d、D、f、F、g、G、M (或 M) 、O (或 o) 、R (或 R) 、s、t、T、u、U、Y (或 y) 。 如需詳細資訊,請參閱 標準日期和時間格式字串

傳回值的每個專案都會使用所提供的特定文化特性資訊來格式化 provider

重要

因為此方法會使用區分文化特性的資料,所以您不應該假設對方法的多個呼叫會傳回相同的資料,除非是代表不因文化特性而異的 provider CultureInfo 物件。 如果使用者覆寫的個別文化設定, provider 或如果系統的文化特性資料發生更新,這個方法所傳回的資料可能會變更 provider

另請參閱

適用於