CultureInfo.DateTimeFormat 屬性

定義

取得或設定 DateTimeFormatInfo,定義日期和時間在文化特性上適當的顯示格式。Gets or sets a DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.

public:
 virtual property System::Globalization::DateTimeFormatInfo ^ DateTimeFormat { System::Globalization::DateTimeFormatInfo ^ get(); void set(System::Globalization::DateTimeFormatInfo ^ value); };
public virtual System.Globalization.DateTimeFormatInfo DateTimeFormat { get; set; }
member this.DateTimeFormat : System.Globalization.DateTimeFormatInfo with get, set
Public Overridable Property DateTimeFormat As DateTimeFormatInfo

屬性值

DateTimeFormatInfo,定義日期和時間在文化特性上適當的顯示格式。A DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.

例外狀況

此屬性設定為 null。The property is set to null.

DateTimeFormat 屬性或 DateTimeFormatInfo 屬性的任何一個已設定,而且 CultureInfo 處於唯讀狀態。The DateTimeFormat property or any of the DateTimeFormatInfo properties is set, and the CultureInfo is read-only.

範例

下列程式碼範例顯示,CultureInfo 也會複製與 CultureInfo相關聯的 DateTimeFormatInfoNumberFormatInfo 實例。The following code example shows that CultureInfo.Clone also clones the DateTimeFormatInfo and NumberFormatInfo instances associated with the CultureInfo.

using namespace System;
using namespace System::Globalization;
int main()
{
   
   // Creates and initializes a CultureInfo.
   CultureInfo^ myCI = gcnew CultureInfo( "en-US",false );
   
   // Clones myCI and modifies the DTFI and NFI instances associated with the clone.
   CultureInfo^ myCIclone = dynamic_cast<CultureInfo^>(myCI->Clone());
   myCIclone->DateTimeFormat->AMDesignator = "a.m.";
   myCIclone->DateTimeFormat->DateSeparator = "-";
   myCIclone->NumberFormat->CurrencySymbol = "USD";
   myCIclone->NumberFormat->NumberDecimalDigits = 4;
   
   // Displays the properties of the DTFI and NFI instances associated with the original and with the clone. 
   Console::WriteLine( "DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE" );
   Console::WriteLine( "DTFI.AMDesignator\t{0}\t\t{1}", myCI->DateTimeFormat->AMDesignator, myCIclone->DateTimeFormat->AMDesignator );
   Console::WriteLine( "DTFI.DateSeparator\t{0}\t\t{1}", myCI->DateTimeFormat->DateSeparator, myCIclone->DateTimeFormat->DateSeparator );
   Console::WriteLine( "NFI.CurrencySymbol\t{0}\t\t{1}", myCI->NumberFormat->CurrencySymbol, myCIclone->NumberFormat->CurrencySymbol );
   Console::WriteLine( "NFI.NumberDecimalDigits\t{0}\t\t{1}", myCI->NumberFormat->NumberDecimalDigits, myCIclone->NumberFormat->NumberDecimalDigits );
}

/*
This code produces the following output.

DTFI/NFI PROPERTY       ORIGINAL        MODIFIED CLONE
DTFI.AMDesignator       AM              a.m.
DTFI.DateSeparator      /               -
NFI.CurrencySymbol      $               USD
NFI.NumberDecimalDigits 2               4
*/
using System;
using System.Globalization;


public class SamplesCultureInfo  {

   public static void Main()  {

      // Creates and initializes a CultureInfo.
      CultureInfo myCI = new CultureInfo("en-US", false);

      // Clones myCI and modifies the DTFI and NFI instances associated with the clone.
      CultureInfo myCIclone = (CultureInfo) myCI.Clone();
      myCIclone.DateTimeFormat.AMDesignator = "a.m.";
      myCIclone.DateTimeFormat.DateSeparator = "-";
      myCIclone.NumberFormat.CurrencySymbol = "USD";
      myCIclone.NumberFormat.NumberDecimalDigits = 4;

      // Displays the properties of the DTFI and NFI instances associated with the original and with the clone. 
      Console.WriteLine( "DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE" );
      Console.WriteLine( "DTFI.AMDesignator\t{0}\t\t{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator );
      Console.WriteLine( "DTFI.DateSeparator\t{0}\t\t{1}", myCI.DateTimeFormat.DateSeparator, myCIclone.DateTimeFormat.DateSeparator );
      Console.WriteLine( "NFI.CurrencySymbol\t{0}\t\t{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol );
      Console.WriteLine( "NFI.NumberDecimalDigits\t{0}\t\t{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits );
      
   }

}

/*
This code produces the following output.

DTFI/NFI PROPERTY       ORIGINAL        MODIFIED CLONE
DTFI.AMDesignator       AM              a.m.
DTFI.DateSeparator      /               -
NFI.CurrencySymbol      $               USD
NFI.NumberDecimalDigits 2               4

*/
Imports System.Globalization


Public Class SamplesCultureInfo
   
   Public Shared Sub Main()
      
      ' Creates and initializes a CultureInfo.
      Dim myCI As New CultureInfo("en-US", False)
      
      ' Clones myCI and modifies the DTFI and NFI instances associated with the clone.
      Dim myCIclone As CultureInfo = CType(myCI.Clone(), CultureInfo)
      myCIclone.DateTimeFormat.AMDesignator = "a.m."
      myCIclone.DateTimeFormat.DateSeparator = "-"
      myCIclone.NumberFormat.CurrencySymbol = "USD"
      myCIclone.NumberFormat.NumberDecimalDigits = 4
      
      ' Displays the properties of the DTFI and NFI instances associated with the original and with the clone. 
      Console.WriteLine("DTFI/NFI PROPERTY" + ControlChars.Tab + "ORIGINAL" + ControlChars.Tab + "MODIFIED CLONE")
      Console.WriteLine("DTFI.AMDesignator" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator)
      Console.WriteLine("DTFI.DateSeparator" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.DateTimeFormat.DateSeparator, myCIclone.DateTimeFormat.DateSeparator)
      Console.WriteLine("NFI.CurrencySymbol" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol)
      Console.WriteLine("NFI.NumberDecimalDigits" + ControlChars.Tab + "{0}" + ControlChars.Tab + ControlChars.Tab + "{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits)

   End Sub

End Class


' This code produces the following output.
'
' DTFI/NFI PROPERTY       ORIGINAL        MODIFIED CLONE
' DTFI.AMDesignator       AM              a.m.
' DTFI.DateSeparator      /               -
' NFI.CurrencySymbol      $               USD
' NFI.NumberDecimalDigits 2               4

備註

OverridesOverrides

使用者可以選擇透過 [控制台] 的 [地區及語言選項] 部分,覆寫與 Windows 目前文化特性相關聯的一些值。The user might choose to override some of the values associated with the current culture of Windows through the regional and language options portion of Control Panel. 例如,使用者可能會選擇以不同的格式來顯示日期,或使用文化特性的預設值以外的貨幣。For example, the user might choose to display the date in a different format or to use a currency other than the default for the culture.

如果 UseUserOverridetrue 而且指定的文化特性與目前的 Windows 文化特性相符,則 CultureInfo 會使用這些覆寫,包括 DateTimeFormat 屬性所傳回之 DateTimeFormatInfo 實例屬性的使用者設定,以及 @no__ 的屬性。NumberFormat 屬性所傳回的 t_5_ 實例。NumberFormatInfoIf UseUserOverride is true and the specified culture matches the current culture of Windows, the CultureInfo uses those overrides, including user settings for the properties of the DateTimeFormatInfo instance returned by the DateTimeFormat property, and the properties of the NumberFormatInfo instance returned by the NumberFormat property. 如果使用者設定與 CultureInfo相關聯的文化特性不相容,例如,如果選取的行事曆不是其中一個 OptionalCalendars,則方法的結果和屬性的值會是未定義的。If the user settings are incompatible with the culture associated with the CultureInfo, for example, if the selected calendar is not one of the OptionalCalendars, the results of the methods and the values of the properties are undefined.

在您的應用程式存取屬性之前,不會計算 DateTimeFormat 屬性和 NumberFormat 屬性的值。The value of the DateTimeFormat property and the NumberFormat property is not calculated until your application accesses the property. 如果使用者可以在應用程式執行時將目前文化特性變更為新的文化特性,然後應用程式會存取 DateTimeFormatNumberFormat 屬性,則應用程式會抓取新文化特性的預設值,而不是原始的覆寫。區域.If the user can change the current culture to a new culture while the application is running and then the application accesses the DateTimeFormat or NumberFormat property, the application retrieves the defaults for the new culture instead of the overrides for the original culture. 若要保留原始目前文化特性的覆寫,應用程式應該先存取 DateTimeFormatNumberFormat 屬性,再變更目前的文化特性。To preserve the overrides for the original current culture, the application should access the DateTimeFormat and NumberFormat properties before changing the current culture.

給呼叫者的注意事項

如果 CalendarTaiwanCalendar,但 CurrentCulture 未設定為中文(臺灣),則名稱為 zh-幼圓,然後 NativeCalendarNameGetEraName(Int32)GetAbbreviatedEraName(Int32) 會傳回空字串("")。If Calendar is the TaiwanCalendar but the CurrentCulture is not set to Chinese (Taiwan), named zh-TW, then NativeCalendarName, GetEraName(Int32), and GetAbbreviatedEraName(Int32) return an empty string ("").

適用於

另請參閱