Export (0) Print
Expand All

CultureInfo.DateTimeFormat Property

Gets or sets a DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.

[Visual Basic]
Public Overridable Property DateTimeFormat As DateTimeFormatInfo
[C#]
public virtual DateTimeFormatInfo DateTimeFormat {get; set;}
[C++]
public: __property virtual DateTimeFormatInfo* get_DateTimeFormat();
public: __property virtual void set_DateTimeFormat(DateTimeFormatInfo*);
[JScript]
public function get DateTimeFormat() : DateTimeFormatInfo;
public function set DateTimeFormat(DateTimeFormatInfo);

Property Value

A DateTimeFormatInfo that defines the culturally appropriate format of displaying dates and times.

Exceptions

Exception Type Condition
ArgumentNullException The property is set to a null reference (Nothing in Visual Basic).
NotSupportedException The CultureInfo is for a neutral culture.
InvalidOperationException The DateTimeFormat property or any of the DateTimeFormatInfo properties is set, and the CultureInfo is read-only.

Remarks

A DateTimeFormatInfo can be created only for the invariant culture or for specific cultures, not for neutral cultures.

The cultures are generally grouped into three sets: the invariant culture, the neutral cultures, and the specific cultures.

The invariant culture is culture-insensitive. You can specify the invariant culture by name using an empty string ("") or by its culture identifier 0x007F. InvariantCulture retrieves an instance of the invariant culture. It is associated with the English language but not with any country/region. It can be used in almost any method in the Globalization namespace that requires a culture. If a security decision depends on a string comparison or a case-change operation, use the InvariantCulture to ensure that the behavior will be consistent regardless of the culture settings of the system. However, the invariant culture must be used only by processes that require culture-independent results, such as system services; otherwise, it produces results that might be linguistically incorrect or culturally inappropriate.

A neutral culture is a culture that is associated with a language but not with a country/region. A specific culture is a culture that is associated with a language and a country/region. For example, "fr" is a neutral culture and "fr-FR" is a specific culture. Note that "zh-CHS" (Simplified Chinese) and "zh-CHT" (Traditional Chinese) are neutral cultures.

The user might choose to override some of the values associated with the current culture of Windows through Regional and Language Options (or Regional Options or Regional Settings) in 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.

If 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, the properties of the NumberFormatInfo instance returned by the NumberFormat property, and the properties of the CompareInfo instance returned by the CompareInfo property. 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.

Example

[Visual Basic, C#, C++] The following code example shows that CultureInfo.Clone also clones the DateTimeFormatInfo and NumberFormatInfo instances associated with the CultureInfo.

[Visual Basic] 
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

[C#] 
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

*/

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;

int main()
{
   // Creates and initializes a CultureInfo.
   CultureInfo* myCI = new CultureInfo(S"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 = S"a.m.";
   myCIclone -> DateTimeFormat -> DateSeparator = S"-";
   myCIclone -> NumberFormat -> CurrencySymbol = S"USD";
   myCIclone -> NumberFormat -> NumberDecimalDigits = 4;

   // Displays the properties of the DTFI and NFI instances associated with the original and with the clone. 
   Console::WriteLine(S"DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE");
   Console::WriteLine(S"DTFI.AMDesignator\t{0}\t\t{1}", myCI -> DateTimeFormat -> AMDesignator, myCIclone -> DateTimeFormat -> AMDesignator);
   Console::WriteLine(S"DTFI.DateSeparator\t{0}\t\t{1}", myCI -> DateTimeFormat -> DateSeparator, myCIclone -> DateTimeFormat -> DateSeparator);
   Console::WriteLine(S"NFI.CurrencySymbol\t{0}\t\t{1}", myCI -> NumberFormat -> CurrencySymbol, myCIclone -> NumberFormat -> CurrencySymbol);
   Console::WriteLine(S"NFI.NumberDecimalDigits\t{0}\t\t{1}", __box(myCI -> NumberFormat -> NumberDecimalDigits), __box(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
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework

See Also

CultureInfo Class | CultureInfo Members | System.Globalization Namespace | DateTimeFormatInfo | GetFormat | NumberFormat

Show:
© 2014 Microsoft