DateTimeFormatInfo Class
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
Provides culture-specific information about the format of date and time values.
Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)
The DateTimeFormatInfo type exposes the following members.
| Name | Description | |
|---|---|---|
|
DateTimeFormatInfo | Initializes a new writable instance of the DateTimeFormatInfo class that is culture-independent (invariant). |
| Name | Description | |
|---|---|---|
|
AbbreviatedDayNames | Gets or sets a one-dimensional array of type String containing the culture-specific abbreviated names of the days of the week. |
|
AbbreviatedMonthGenitiveNames | Gets or sets a string array of abbreviated month names associated with the current DateTimeFormatInfo object. |
|
AbbreviatedMonthNames | Gets or sets a one-dimensional string array that contains the culture-specific abbreviated names of the months. |
|
AMDesignator | Gets or sets the string designator for hours that are "ante meridiem" (before noon). |
|
Calendar | Gets or sets the calendar to use for the current culture. |
|
CalendarWeekRule | Gets or sets a value that specifies which rule is used to determine the first calendar week of the year. |
|
CurrentInfo | Gets a read-only DateTimeFormatInfo object that formats values based on the current culture. |
|
DateSeparator | Gets or sets the string that separates the components of a date, that is, the year, month, and day. |
|
DayNames | Gets or sets a one-dimensional string array that contains the culture-specific full names of the days of the week. |
|
FirstDayOfWeek | Gets or sets the first day of the week. |
|
FullDateTimePattern | Gets or sets the custom format string for a long date and long time value. |
|
InvariantInfo | Gets the default read-only DateTimeFormatInfo object that is culture-independent (invariant). |
|
IsReadOnly | Gets a value indicating whether the DateTimeFormatInfo object is read-only. |
|
LongDatePattern | Gets or sets the custom format string for a long date value. |
|
LongTimePattern | Gets or sets the custom format string for a long time value. |
|
MonthDayPattern | Gets or sets the custom format string for a month and day value. |
|
MonthGenitiveNames | Gets or sets a string array of month names associated with the current DateTimeFormatInfo object. |
|
MonthNames | Gets or sets a one-dimensional array of type String containing the culture-specific full names of the months. |
|
NativeCalendarName | Gets the native name of the calendar associated with the current DateTimeFormatInfo object. |
|
PMDesignator | Gets or sets the string designator for hours that are "post meridiem" (after noon). |
|
RFC1123Pattern | Gets the custom format string for a time value that is based on the Internet Engineering Task Force (IETF) Request for Comments (RFC) 1123 specification. |
|
ShortDatePattern | Gets or sets the custom format string for a short date value. |
|
ShortestDayNames | Gets or sets a string array of the shortest unique abbreviated day names associated with the current DateTimeFormatInfo object. |
|
ShortTimePattern | Gets or sets the custom format string for a short time value. |
|
SortableDateTimePattern | Gets the custom format string for a sortable date and time value. |
|
TimeSeparator | Gets or sets the string that separates the components of time, that is, the hour, minutes, and seconds. |
|
UniversalSortableDateTimePattern | Gets the custom format string for a universal, sortable date and time string. |
|
YearMonthPattern | Gets or sets the custom format string for a year and month value. |
| Name | Description | |
|---|---|---|
|
Clone | Creates a shallow copy of the DateTimeFormatInfo. |
|
Equals(Object) | Determines whether the specified Object is equal to the current Object. (Inherited from Object.) |
|
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) |
|
GetAbbreviatedDayName | Returns the culture-specific abbreviated name of the specified day of the week based on the culture associated with the current DateTimeFormatInfo object. |
|
GetAbbreviatedEraName | Returns the string containing the abbreviated name of the specified era, if an abbreviation exists. |
|
GetAbbreviatedMonthName | Returns the culture-specific abbreviated name of the specified month based on the culture associated with the current DateTimeFormatInfo object. |
|
GetAllDateTimePatterns() | Returns all the standard patterns in which date and time values can be formatted. |
|
GetAllDateTimePatterns(Char) | Returns all the patterns in which date and time values can be formatted using the specified standard format string. |
|
GetDayName | Returns the culture-specific full name of the specified day of the week based on the culture associated with the current DateTimeFormatInfo object. |
|
GetEra | Returns the integer representing the specified era. |
|
GetEraName | Returns the string containing the name of the specified era. |
|
GetFormat | Returns an object of the specified type that provides a date and time formatting service. |
|
GetHashCode | Serves as a hash function for a particular type. (Inherited from Object.) |
|
GetInstance | Returns the DateTimeFormatInfo object associated with the specified IFormatProvider. |
|
GetMonthName | Returns the culture-specific full name of the specified month based on the culture associated with the current DateTimeFormatInfo object. |
|
GetShortestDayName | Obtains the shortest abbreviated day name for a specified day of the week associated with the current DateTimeFormatInfo object. |
|
GetType | Gets the Type of the current instance. (Inherited from Object.) |
|
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) |
|
ReadOnly | Returns a read-only DateTimeFormatInfo wrapper. |
|
SetAllDateTimePatterns | Sets the custom date and time format strings that correspond to a specified standard format string. |
|
ToString | Returns a string that represents the current object. (Inherited from Object.) |
The properties of the DateTimeFormatInfo class contain culture-specific information for formatting or parsing date and time values such as the following:
-
The patterns used to format date values.
-
The patterns used to format time values.
-
The names of the days of the week.
-
The names of the months of the year.
-
The A.M. and P.M. designators used in time values.
-
The calendar in which dates are expressed.
This behavior is particularly important in the case of serialization. The .NET Framework persists all overridable settings, instead of persisting only the settings that are accessed at the time of serialization.
Instantiating a DateTimeFormatInfo Object
A DateTimeFormatInfo object can represent the formatting conventions of the invariant culture, a specific culture, a neutral culture, or the current culture. This section discusses how to instantiate each type of DateTimeFormatInfo object.
Instantiating a DateTimeFormatInfo Object for the Invariant Culture
The invariant culture represents a culture that is culture-insensitive. It is based on the English language, but not on any specific English-speaking country/region. Although the data of specific cultures can be dynamic and can change to reflect new cultural conventions or user preferences, the data of the invariant culture does not change. You can instantiate a DateTimeFormatInfo object that represents the formatting conventions of the invariant culture in the following ways:
-
By retrieving the value of the InvariantInfo property. The returned DateTimeFormatInfo object is read-only.
-
By calling the DateTimeFormatInfo constructor. The returned DateTimeFormatInfo object is read/write.
-
By retrieving the value of the DateTimeFormat property from the CultureInfo object that is returned by the CultureInfo.InvariantCulture property. The returned DateTimeFormatInfo object is read-only.
The following example uses each of these methods to instantiate a DateTimeFormatInfo object that represents the invariant culture. It then indicates whether the object is read-only.
System.Globalization.DateTimeFormatInfo dtfi; dtfi = System.Globalization.DateTimeFormatInfo.InvariantInfo; Console.WriteLine(dtfi.IsReadOnly); dtfi = new System.Globalization.DateTimeFormatInfo(); Console.WriteLine(dtfi.IsReadOnly); dtfi = System.Globalization.CultureInfo.InvariantCulture.DateTimeFormat; Console.WriteLine(dtfi.IsReadOnly); // The example displays the following output: // True // False // True
Instantiating a DateTimeFormatInfo Object for a Specific Culture
A specific culture represents a language that is spoken in a particular country/region. For example, en-US is a specific culture that represents the English language spoken in the United States, and en-CA is a specific culture that represents the English language spoken in Canada. You can instantiate a DateTimeFormatInfo object that represents the formatting conventions of a specific culture in the following ways:
-
By retrieving the value of the CurrentInfo property. This returns a read-only DateTimeFormatInfo object that represents the formatting conventions of the current culture.
-
By instantiating a CultureInfo object that represents a specific culture and retrieving the value of its CultureInfo.DateTimeFormat property. Whether the DateTimeFormatInfo object is read/write or read-only depends on how its associated CultureInfo object is instantiated:
-
If the object is instantiated by calling the CultureInfo.GetCultureInfo property, it is read-only.
-
If the object is instantiated by calling the CultureInfo.CultureInfo constructor or the CultureInfo.CreateSpecificCulture method, it is read/write.
-
The following example illustrates each of these ways to instantiate a DateTimeFormatInfo object and indicates whether the resulting object is read-only.
System.Globalization.CultureInfo ci = null; System.Globalization.DateTimeFormatInfo dtfi = null; // Get the current culture//s DateTimeFormatInfo object. dtfi = System.Globalization.DateTimeFormatInfo.CurrentInfo; Console.WriteLine("{0} from CurrentInfo property: {1}", System.Globalization.CultureInfo.CurrentCulture.Name, dtfi.IsReadOnly); // Instantiate a culture using CreateSpecificCulture. ci = System.Globalization.CultureInfo.CreateSpecificCulture("en-US"); dtfi = ci.DateTimeFormat; Console.WriteLine("{0} from CreateSpecificCulture: {1}", ci.Name, dtfi.IsReadOnly); // Instantiate a culture using the CultureInfo constructor. ci = new System.Globalization.CultureInfo("en-CA"); dtfi = ci.DateTimeFormat; Console.WriteLine("{0} from CultureInfo constructor: {1}", ci.Name, dtfi.IsReadOnly); ci = System.Globalization.CultureInfo.GetCultureInfo("en-AU"); dtfi = ci.DateTimeFormat; Console.WriteLine("{0} from GetCultureInfo: {1}", ci.Name, dtfi.IsReadOnly); // The example displays the following output: // en-US from CurrentInfo property: True // en-US from CreateSpecificCulture: False // en-CA from CultureInfo constructor: False // en-AU from GetCultureInfo: True
Instantiating a DateTimeFormatInfo Object for a Neutral Culture
A neutral culture represents a culture or language that is independent of a country/region; it is typically the parent of one or more specific cultures. For example, Fr is a neutral culture for the French language and the parent of the fr-FR culture. You can instantiate a DateTimeFormatInfo object that represents the formatting conventions of a neutral culture in the following ways:
-
By retrieving a CultureInfo object that represents a neutral culture from the CultureInfo.Parent property of its specific culture, and then retrieving the value of its CultureInfo.DateTimeFormat property.
Caution
In general, the parent of a neutral culture is the invariant culture. However, in some cases, the parent of a neutral culture is another neutral culture. For example, sr-Cyrl (or Serbian Cyrillic) is a neutral culture that represents the Serbian language using the Cyrillic character set. Its parent is another neutral culture, sr (or Serbian), which represents the Serbian language.
-
By instantiating a neutral culture by calling the CultureInfo.CultureInfo class constructor, the CultureInfo.CreateSpecificCulture method, or the CultureInfo.GetCultureInfo method to instantiate a CultureInfo object that represents a neutral culture and retrieving the DateTimeFormatInfo object from its CultureInfo.DateTimeFormat property. The following example illustrates these ways of instantiating a CultureInfo object that represents a neutral culture.
System.Globalization.CultureInfo specific, neutral; System.Globalization.DateTimeFormatInfo dtfi; specific = System.Globalization.CultureInfo.CreateSpecificCulture("fr-FR"); neutral = specific.Parent; dtfi = neutral.DateTimeFormat; neutral = new System.Globalization.CultureInfo("fr"); dtfi = neutral.DateTimeFormat; neutral = System.Globalization.CultureInfo.CreateSpecificCulture("fr"); dtfi = neutral.DateTimeFormat; neutral = System.Globalization.CultureInfo.GetCultureInfo("fr"); dtfi = neutral.DateTimeFormat; dtfi = System.Globalization.CultureInfo.GetCultureInfo("fr-FR").Parent.DateTimeFormat;
Instantiating a DateTimeFormatInfo Object for the Current Culture
You can instantiate a DateTimeFormatInfo object that represents the formatting conventions of the current culture in the following ways:
-
By retrieving the value of the CurrentInfo property. The returned DateTimeFormatInfo object is read-only.
-
By retrieving the value of the DateTimeFormat property from the CultureInfo object that is returned by the CultureInfo.CurrentCulture property. The returned DateTimeFormatInfo object is read-only.
The following example uses each of these methods to instantiate a DateTimeFormatInfo object that represents the formatting conventions of the current culture. It then indicates whether the object is read-only.
DateTimeFormatInfo and Dynamic Data
The data for formatting date and time values provided by the DateTimeFormatInfo class is dynamic, just like cultural data provided by the CultureInfo class. You should not make any assumptions about the stability of values for DateTimeFormatInfo objects that are associated with particular CultureInfo objects. Only the data provided by the invariant culture and its associated DateTimeFormatInfo object is stable. Other data can change between application sessions or even while your application is running. There are four major sources of change:
-
System updates. Cultural preferences such as the preferred calendar or customary date and time formats change over time. When this happens, Windows Update includes changes to the DateTimeFormatInfo property value for a particular culture.
-
Replacement cultures. The CultureAndRegionInfoBuilder class can be used to replace the data of an existing culture.
-
Cascading changes to property values. A number of culture-related properties can change at run time, which, in turn, causes DateTimeFormatInfo data to change. For example, the current culture can be changed either programmatically or through user action. When this happens, the DateTimeFormatInfo object returned by the CurrentInfo property changes to an object associated with the current culture. Similarly, a culture's calendar can change, which can result in changes to numerous DateTimeFormatInfo property values.
-
User preferences. Users of your application might choose to override some of the values associated with the current system culture through the regional and language options in Control Panel. For example, users might choose to display the date in a different format. If the CultureInfo.UseUserOverride property is set to true, the properties of the CultureInfo.DateTimeFormat, CultureInfo.NumberFormat, and CultureInfo.TextInfo objects are also retrieved from the user settings. If the user settings are incompatible with the culture associated with the CultureInfo object (for example, if the selected calendar is not one of the calendars indicated by OptionalCalendars), the results of the methods and the values of the properties are undefined.
To minimize the possibility of inconsistent data, all user-overridable properties of a DateTimeFormatInfo object are initialized when the object is created. There is still a possibility of inconsistency, because neither object creation nor the user override process is atomic and the relevant values can change during object creation. However, this situation should be extremely rare.
You can control whether user overrides are reflected in DateTimeFormatInfo objects that represent the same culture as the system culture. The following table lists the ways in which a DateTimeFormatInfo object can be retrieved and indicates whether the resulting object reflects user overrides.
|
Source of CultureInfo and DateTimeFormatInfo object |
Reflects user overrides |
|---|---|
|
CultureInfo.CurrentCulture.DateTimeFormat |
Yes |
|
DateTimeFormatInfo.CurrentInfo property |
Yes |
|
Yes |
|
|
CultureInfo.GetCultureInfo method |
No |
|
CultureInfo.CultureInfo(String) constructor |
Yes |
|
CultureInfo.CultureInfo(String, Boolean) constructor |
Depends on value of useUserOverride parameter |
Unless there is a compelling reason to do otherwise, you should respect user overrides when you use the DateTimeFormatInfo object in client applications to format and parse user input or to display data. For server applications or unattended applications, you should not. However, if you are using the DateTimeFormatInfo object either explicitly or implicitly to persist date and time data in string form, you should either use a DateTimeFormatInfo object that reflects the formatting conventions of the invariant culture, or you should specify a custom date and time format string that you use regardless of culture.
Formatting Dates and Times
Most DateTimeFormatInfo properties are related to formatting date and time values. Some of these, such as AbbreviatedDayNames or MonthNames, are calendar-related and are used for long date and time formats. Others, such as FullDateTimePattern and ShortDatePattern, are custom format strings that specify the format of the result string. The standard numeric format strings, such as "d", "D", "f", and "F", are aliases that correspond to particular DateTimeFormatInfo format pattern properties. The precise correspondence of standard format strings to DateTimeFormatInfo properties is documented in the Standard Date and Time Format Strings topic.
The format of some DateTimeFormatInfo format string properties, including the RFC1123Pattern, SortableDateTimePattern, and UniversalSortableDateTimePattern properties, conform to a defined standard and therefore cannot be modified. However, you can modify the result string produced by standard date and time format strings by modifying the associated properties of a writable DateTimeFormatInfo object. To determine if a DateTimeFormatInfo object is writable, use the IsReadOnly property. The following example illustrates how to change the format of a result string produced by the "d" standard format string. It changes the associated ShortDatePattern property for the en-US or English (United States) culture from "M/d/yyyy" to "yyyy'-'MM'-'dd" and uses the "d" standard format string to display the date both before and after the ShortDatePattern property is changed..
The following example uses reflection to get the properties of the DateTimeFormatInfo object for the English (United States) culture. It displays the value of those properties that contain custom format strings and uses those strings to display formatted dates.
using System; using System.Globalization; using System.Reflection; public class Example { public static void Main() { // Get the properties of an en-US DateTimeFormatInfo object. DateTimeFormatInfo dtfi = CultureInfo.GetCultureInfo("en-US").DateTimeFormat; Type typ = dtfi.GetType(); PropertyInfo[] props = typ.GetProperties(); DateTime value = new DateTime(2012, 5, 28, 11, 35, 0); foreach (var prop in props) { // Is this a format pattern-related property? if (prop.Name.Contains("Pattern")) { string fmt = prop.GetValue(dtfi, null).ToString(); Console.WriteLine("{0,-33} {1} \n{2,-37}Example: {3}\n", prop.Name + ":", fmt, "", value.ToString(fmt)); } } } } // The example displays the following output: // FullDateTimePattern: dddd, MMMM dd, yyyy h:mm:ss tt // Example: Monday, May 28, 2012 11:35:00 AM // // LongDatePattern: dddd, MMMM dd, yyyy // Example: Monday, May 28, 2012 // // LongTimePattern: h:mm:ss tt // Example: 11:35:00 AM // // MonthDayPattern: MMMM dd // Example: May 28 // // RFC1123Pattern: ddd, dd MMM yyyy HH':'mm':'ss 'GMT' // Example: Mon, 28 May 2012 11:35:00 GMT // // ShortDatePattern: M/d/yyyy // Example: 5/28/2012 // // ShortTimePattern: h:mm tt // Example: 11:35 AM // // SortableDateTimePattern: yyyy'-'MM'-'dd'T'HH':'mm':'ss // Example: 2012-05-28T11:35:00 // // UniversalSortableDateTimePattern: yyyy'-'MM'-'dd HH':'mm':'ss'Z' // Example: 2012-05-28 11:35:00Z // // YearMonthPattern: MMMM, yyyy // Example: May, 2012
Windows 8 Release Preview, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 SP2, Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.