CultureInfo.DateTimeFormat 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得或設定 DateTimeFormatInfo,定義日期和時間在文化特性上適當的顯示格式。
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,定義用於顯示日期和時間的適當文化特性格式。
例外狀況
此屬性設定為 null。
DateTimeFormat 屬性或 DateTimeFormatInfo 屬性的任何一個已設定,而且 CultureInfo 處於唯讀狀態。
範例
下列程式代碼範例顯示 CultureInfo.Clone 也會複製DateTimeFormatInfo與 相關聯的 CultureInfo和 NumberFormatInfo 實例。
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
備註
您可以選擇透過 控制台 的區域和語言選項部分,覆寫與目前 Windows 文化特性相關聯的一些值。 例如,使用者可能會選擇以不同的格式顯示日期,或使用文化特性預設值以外的貨幣。
如果 是 UseUserOverridetrue
且指定的文化特性符合 Windows 的目前文化特性,則會CultureInfo使用這些覆寫,包括屬性所DateTimeFormat傳回之實例屬性DateTimeFormatInfo的用戶設定,以及 屬性所NumberFormat傳回之實例的屬性NumberFormatInfo。 如果使用者設定與相關聯的 CultureInfo文化特性不相容,例如,如果選取的行事曆不是 其中 OptionalCalendars一個 ,則方法的結果和屬性的值是未定義的。
在您的應用程式存取屬性之前,不會計算屬性和 NumberFormat 屬性的值DateTimeFormat。 如果使用者在執行應用程式時,可以將目前的文化特性變更為新的文化特性,然後應用程式存取 DateTimeFormat 或 NumberFormat 屬性,則應用程式會擷取新文化特性的預設值,而不是原始文化特性的覆寫。 若要保留原始目前文化特性的覆寫,應用程式應該先存取 和 NumberFormat 屬性,DateTimeFormat再變更目前的文化特性。
給呼叫者的注意事項
如果 Calendar 是 ,TaiwanCalendar但 CurrentCulture 未設定為 (臺灣) ,名為 zh-TW,則 、 GetEraName(Int32)和 會NativeCalendarNameGetAbbreviatedEraName(Int32)傳回空字串 (“”) 。
適用於
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應