CultureInfo.DateTimeFormat Property

July 28, 2014

Gets or sets a DateTimeFormatInfo object that defines the culturally appropriate format for converting dates and times to strings.

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

public virtual DateTimeFormatInfo DateTimeFormat { get; set; }

Property Value

Type: System.Globalization.DateTimeFormatInfo
An object that defines the culturally appropriate format for converting dates and times to strings.

ExceptionCondition
ArgumentNullException

The property is set to null.

InvalidOperationException

The DateTimeFormat property or any of the DateTimeFormatInfo properties is set, and the CultureInfo is read-only.

The user might choose to override some of the values associated with the current system culture. 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. The DateTimeFormatInfo object returned by the current culture's DateTimeFormat property reflects these overrides.

The values of the DateTimeFormat property and the NumberFormat property are not populated until the property is accessed. If the application changes 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. To preserve the overrides for the original current culture, the application should 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.GetEraName and DateTimeFormatInfo.GetAbbreviatedEraName return an empty string ("").

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


using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // Creates and initializes a CultureInfo object.
      CultureInfo myCI = new CultureInfo("en-US");

      // Clones myCI and modifies the DTFI and NFI instances associated with the clone.
      CultureInfo myCIclone = (CultureInfo)myCI.Clone();
      myCIclone.DateTimeFormat.AMDesignator = "a.m.";
      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. 
      outputBlock.Text += "DTFI/NFI PROPERTY\tORIGINAL\tMODIFIED CLONE" + "\n";
      outputBlock.Text += String.Format("DTFI.AMDesignator\t{0}\t\t{1}", myCI.DateTimeFormat.AMDesignator, myCIclone.DateTimeFormat.AMDesignator) + "\n";
      outputBlock.Text += String.Format("NFI.CurrencySymbol\t{0}\t\t{1}", myCI.NumberFormat.CurrencySymbol, myCIclone.NumberFormat.CurrencySymbol) + "\n";
      outputBlock.Text += String.Format("NFI.NumberDecimalDigits\t{0}\t\t{1}", myCI.NumberFormat.NumberDecimalDigits, myCIclone.NumberFormat.NumberDecimalDigits) + "\n";
   }
}
/*
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 Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft