Export (0) Print
Expand All

CultureInfo.DateTimeFormat Property

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

Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)

public virtual DateTimeFormatInfo DateTimeFormat { get; set; }
/** @property */
public DateTimeFormatInfo get_DateTimeFormat ()

/** @property */
public void set_DateTimeFormat (DateTimeFormatInfo value)

public function get DateTimeFormat () : DateTimeFormatInfo

public function set DateTimeFormat (value : DateTimeFormatInfo)

Property Value

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

Exception typeCondition

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.

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.

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.

Overrides

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

The value of the DateTimeFormat property and the NumberFormat property is not calculated until you access the property. If you use the Control Panel to change the current culture to a new culture while your application is running then access the DateTimeFormat or NumberFormat property, your application will get the defaults for the new culture not the overrides for the original culture. If you want to preserve the overrides for the original current culture access the DateTimeFormat and NumberFormat properties before changing the current culture.

Notes to Callers If DateTimeFormatInfo.Calendar is the TaiwanCalendar but the Thread.CurrentCulture is not "zh-TW", then DateTimeFormatInfo.NativeCalendarName, DateTimeFormatInfo.GetEraName, and DateTimeFormatInfo.GetAbbreviatedEraName return an empty string ("").

The following code example shows that CultureInfo.Clone also clones the DateTimeFormatInfo and NumberFormatInfo instances associated with the CultureInfo.

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

*/

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

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft