CultureInfo.DateTimeFormat 属性

获取或设置 DateTimeFormatInfo,它定义适合区域性的、显示日期和时间的格式。

**命名空间:**System.Globalization
**程序集:**mscorlib(在 mscorlib.dll 中)

语法

声明
Public Overridable Property DateTimeFormat As DateTimeFormatInfo
用法
Dim instance As CultureInfo
Dim value As DateTimeFormatInfo

value = instance.DateTimeFormat

instance.DateTimeFormat = value
public virtual DateTimeFormatInfo DateTimeFormat { get; set; }
public:
virtual property DateTimeFormatInfo^ DateTimeFormat {
    DateTimeFormatInfo^ get ();
    void set (DateTimeFormatInfo^ value);
}
/** @property */
public DateTimeFormatInfo get_DateTimeFormat ()

/** @property */
public void set_DateTimeFormat (DateTimeFormatInfo value)
public function get DateTimeFormat () : DateTimeFormatInfo

public function set DateTimeFormat (value : DateTimeFormatInfo)

属性值

一个 DateTimeFormatInfo,它定义适合区域性的、显示日期和时间的格式。

异常

异常类型 条件

ArgumentNullException

该属性设置为 空引用(在 Visual Basic 中为 Nothing)。

NotSupportedException

CultureInfo 是用于非特定区域性的。

InvalidOperationException

设置 DateTimeFormat 属性或设置任何 DateTimeFormatInfo 属性,并且 CultureInfo 是只读的。

备注

只能为固定区域性或特定区域性(而不能为非特定区域性)创建 DateTimeFormatInfo

这些区域性通常分为三个集合:固定区域性、非特定区域性和特定区域性。

固定区域性不区分区域性。可以使用空字符串 ("") 按名称或者按区域性标识符 0x007F 来指定固定区域性。InvariantCulture 检索固定区域性的实例。它与英语语言关联,但不与任何国家/地区关联。它几乎可用在要求区域性的“全局化”命名空间中的所有方法中。

非特定区域性是与某种语言关联但不与国家/地区关联的区域性。特定区域性是与某种语言和某个国家/地区关联的区域性。例如,“fr”是非特定区域性,而“fr-FR”是特定区域性。注意,“zh-CHS”(简体中文)和“zh-CHT”(繁体中文)均为非特定区域性。

重写

用户可以通过“控制面板”中的“区域和语言选项”(或“区域选项”或“区域设置”)选择重写某些与 Windows 的当前区域性关联的值。例如,用户可能选择以另一种格式显示日期,或选择使用区域性默认设置以外的货币。

如果 UseUserOverridetrue 并且指定的区域性与当前 Windows 的区域性匹配,则 CultureInfo 使用那些重写,包括以下属性的用户设置:由 DateTimeFormat 属性返回的 DateTimeFormatInfo 实例的属性以及由 NumberFormat 属性返回的 NumberFormatInfo 实例的属性。如果用户设置与 CultureInfo 的关联区域性不兼容(例如选定的日历不属于 OptionalCalendars 其中之一),则方法结果和属性值是未定义的。

DateTimeFormat 属性和 NumberFormat 属性的值在您访问该属性之后才会计算。如果在应用程序正在运行时使用控制面板将当前区域性更改为新的区域性,然后访问 DateTimeFormatNumberFormat 属性,则应用程序将获得新的区域性的默认设置,而不是原始区域性的重写设置。如果要保留当前原始区域性的重写设置,则应在更改当前区域性之前访问 DateTimeFormatNumberFormat 属性。

给调用者的说明 如果 DateTimeFormatInfo.CalendarTaiwanCalendar,但 Thread.CurrentCulture 不是“zh-TW”,则 DateTimeFormatInfo.NativeCalendarNameDateTimeFormatInfo.GetEraNameDateTimeFormatInfo.GetAbbreviatedEraName 将返回一个空字符串 ("")。

示例

下面的代码示例显示 CultureInfo.Clone 也会克隆与 CultureInfo 关联的 DateTimeFormatInfoNumberFormatInfo 实例。

Imports System
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 'Main 

End Class 'SamplesCultureInfo


' 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

*/
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
*/
import System.* ;
import System.Globalization.* ;

public class SamplesCultureInfo
{
    public static void main(String[] args)
    {
        // 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.get_DateTimeFormat().set_AMDesignator( "a.m.");
        myCIclone.get_DateTimeFormat().set_DateSeparator ("-");
        myCIclone.get_NumberFormat().set_CurrencySymbol("USD");
        myCIclone.get_NumberFormat().set_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.get_DateTimeFormat().get_AMDesignator(), 
            myCIclone.get_DateTimeFormat().get_AMDesignator());
        Console.WriteLine("DTFI.DateSeparator\t{0}\t\t{1}", 
            myCI.get_DateTimeFormat().get_DateSeparator(), 
            myCIclone.get_DateTimeFormat().get_DateSeparator());
        Console.WriteLine("NFI.CurrencySymbol\t{0}\t\t{1}", 
            myCI.get_NumberFormat().get_CurrencySymbol(), 
            myCIclone.get_NumberFormat().get_CurrencySymbol());
        Console.WriteLine("NFI.NumberDecimalDigits\t{0}\t\t{1}",
            System.Convert.ToString(
            myCI.get_NumberFormat().get_NumberDecimalDigits()), 
            System.Convert.ToString(
            myCIclone.get_NumberFormat().get_NumberDecimalDigits()));
    } //main
} //SamplesCultureInfo

/*
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 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Mobile for Smartphone、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

.NET Compact Framework

受以下版本支持:2.0、1.0

请参见

参考

CultureInfo 类
CultureInfo 成员
System.Globalization 命名空间
DateTimeFormatInfo
GetFormat
NumberFormat