Mono Class Library: System.Globalization Namespace

System.Globalization.DateTimeFormatInfo Class

Defines culture-specific formats and patterns for DateTime values. [Edit]

See Also: DateTimeFormatInfo Members

[System.Runtime.InteropServices.ComVisible(true)]
public sealed class DateTimeFormatInfo : ICloneable, IFormatProvider

Thread Safety

This type is safe for multithreaded operations.

Remarks

DateTime values are formatted by the DateTime.ParseExact(string, string, IFormatProvider) and DateTime.ToString methods according to standard or custom patterns stored in the properties of a System.Globalization.DateTimeFormatInfo instance. The standard patterns can be accessed and modified through the associated System.Globalization.DateTimeFormatInfo properties.

Note: The format patterns and properties of a read-only System.Globalization.DateTimeFormatInfo instance cannot be changed. To determine whether a System.Globalization.DateTimeFormatInfo instance is read-only, use the DateTimeFormatInfo.IsReadOnly property.

Date and time format patterns are specified using strings called format specifiers. A string is interpreted as standard format specifier if it contains exactly one standard format specifier character. If the string contains a single character and that character is not one of the standard format specifiers, an exception is thrown. If the string contains two or more characters, even if the extra characters are white spaces, the string is interpreted as a custom format specifier. Format specifiers and format patterns are case-sensitive; for example, 'g' and 'G' represent different patterns.

The following table shows the standard format specifiers and the associated format pattern defined for the invariant culture. The exact pattern produced by a format specifier is influenced by culture-specific date and/or time settings on the current system; computers with different date and time settings might display different patterns. The asterisk at the end of a format pattern indicates that the preceding character can be repeated without changing the meaning of the pattern. For example, the pattern "HH*" indicates that the strings "HH", "HHH", "HHHH", and "HHHHH" produce the same result when used with DateTime.ParseExact(string, string, IFormatProvider) and DateTime.ToString methods.

Format SpecifierFormat PatternDescription
dMM/dd/yyyyThe full date in numeric format (DateTimeFormatInfo.ShortDatePattern ).
Ddddd*, dd MMMM* yyyyThe full date including the day of the week and the name of the month (DateTimeFormatInfo.LongDatePattern ).
fdddd*, dd MMMM* yyyy HH*:mm*The full date and time, including the day of the week and the name of the month (DateTimeFormatInfo.LongDatePattern combined with DateTimeFormatInfo.ShortTimePattern ).
Fdddd*, dd MMMM* yyyy HH*:mm*:ss*The full date and time, including the seconds (DateTimeFormatInfo.FullDateTimePattern equivalent to DateTimeFormatInfo.LongDatePattern combined with DateTimeFormatInfo.LongTimePattern ).
gMM/dd/yyyy HH*:mm*A general date pattern including the short time form (DateTimeFormatInfo.ShortDatePattern combined with DateTimeFormatInfo.ShortTimePattern ).
GMM/dd/yyyy HH*:mm*:ss*A general date pattern including the long time form (DateTimeFormatInfo.ShortDatePattern combined with DateTimeFormatInfo.LongTimePattern ).
m, MMMMM* ddThe full name of the month and the date (DateTimeFormatInfo.MonthDayPattern ).
tHH*:mm*The time in short format (DateTimeFormatInfo.ShortTimePattern ).
THH*:mm*:ss*The time in long format (DateTimeFormatInfo.LongTimePattern ).
Udddd*, dd MMMM* yyyy HH*:mm*:ss*The full date and time, including the seconds, in the Gregorian calendar ( DateTimeFormatInfo.FullDateTimePattern ).
y, Yyyyy MMMM*The full name of the month and the year in four-digit format (DateTimeFormatInfo.YearMonthPattern ).
The following table lists custom format specifiers that can be combined to construct custom patterns. If the custom pattern contains white space characters, characters enclosed in single or double quotation marks, or characters not defined in the following table, these characters are considered literals and are included in the output string unchanged.
Note: See the string class for the list of white space characters.
Format PatternDescriptionExamples
dThe day of the month as a value in the range 1-31, inclusive. Single-digit days do not have a leading zero.

1

22

ddThe day of the month as a value in the range 1-31, inclusive. Single-digit days have a leading zero.

01

22

dddThe abbreviated name of the day of the week, as defined in DateTimeFormatInfo.AbbreviatedDayNames .Mon
dddd*The full name of the day of the week, as defined in DateTimeFormatInfo.DayNames .Monday
MThe numeric month as a value in the range 1-12, inclusive. Single-digit months do not have a leading zero.

2

11

MMThe numeric month as a value in the range 1-12, inclusive. Single-digit months have a leading zero.

02

11

MMMThe abbreviated name of the month, as defined in DateTimeFormatInfo.AbbreviatedMonthNames .Feb
MMMM*The full name of the month, as defined in DateTimeFormatInfo.MonthNames .February
yThe year without the century (two-digit). If the value is less than 10, the year is displayed with no leading zero.

0

3

yyThe year without the century (two-digit). If the year without the century is less than 10, the year is displayed with a leading zero.

00

03

yyyyThe year including the century in four digits.

2000

2003

g*The name of a period or era (such as "A.D." or "B.C."). This pattern is ignored if the date to be formatted does not have an associated period or era string.A.D.
hThe hour within a 12-hour range as a value in the range 1-12, inclusive. Single-digit hours do not have a leading zero.
Note: The value represents whole hours passed since either midnight (12) or noon (12). To distinguish between values occurring before and after noon, include the "t" or "tt*" custom format specifier.

3

11

hh*The hour within a 12-hour range as a value in the range 1-12, inclusive. Single-digit hours have a leading zero.
Note: The value represents whole hours passed since either midnight (12) or noon (12). To distinguish between values occurring before and after noon, include the "t" or "tt*" custom format specifier.

03

11

HThe hour as a value in the range 0-23, inclusive. Single-digit hours do not have a leading zero.
Note: The value represents whole hours passed since midnight.

3

13

HH*The hour as a value in the range 0 and 23, inclusive. Single-digit hours have a leading zero.
Note: The value represents whole hours passed since midnight.

03

13

mThe minute as a value in the range 0-59, inclusive. Single-digit minutes do not have a leading zero.
Note: The value represents whole minutes passed since the last hour.

5

15

mm*The minute as a value in the range 0-59, inclusive. Single-digit minutes have a leading zero.
Note: The value represents whole minutes passed since the last hour.

05

15

sThe second as a value in the range 0-59, inclusive. Single-digit seconds do not have a leading zero.
Note: The value represents whole seconds passed since the last minute.

1

30

ss*The second as a value in the range 0-59, inclusive. Single-digit seconds have a leading zero.
Note: The value represents whole seconds passed since the last minute.

01

30

f Displays fractional seconds represented in one digit.1
ff Displays fractional seconds represented in two digits.01
fff Displays fractional seconds represented in three digits.001
ffff Displays fractional seconds represented in four digits.0001
fffff Displays fractional seconds represented in five digits.00001
ffffff Displays fractional seconds represented in six digits.000001
fffffff Displays fractional seconds represented in seven digits.0000001
tThe first character of the AM/PM designator defined in the System.Globalization.DateTimeFormatInfo property DateTimeFormatInfo.AMDesignator or DateTimeFormatInfo.PMDesignator.
Note: If the total number of hours passed since midnight is less than 12, the A.M. designator is used; otherwise the P.M. designator is used.

A

P

tt*The AM/PM designator defined in the System.Globalization.DateTimeFormatInfo property DateTimeFormatInfo.AMDesignator or DateTimeFormatInfo.PMDesignator.
Note: If the total number of hours passed since midnight is less than 12, the A.M. designator is used; otherwise the P.M. designator is used.

AM

PM

zThe time zone offset (hour only) from the universal time coordinate (UTC) time (Greenwich Mean Time) as a value in the range -12 to +13, inclusive. Single-digit hours do not have a leading zero.
Note: The value always includes a leading sign (zero is '+0'), indicating hours ahead of UTC time (+) or hours behind UTC time (-). The offset takes Daylight Savings Time into account.
-8
zzThe time zone offset (hour only) from the UTC time (Greenwich Mean Time) as a value in the range -12 to +13, inclusive. Single-digit hours have a leading zero.
Note: The value always includes a leading sign (zero is '+0'), indicating hours ahead of UTC time (+) or hours behind UTC time (-). The offset takes Daylight Savings Time into account.
-08
zzz* The full time zone offset (hour and minutes) from the UTC time (Greenwich Mean Time) as a value in the range -12:00 to +13:00, inclusive. Single-digit hours and minutes have leading zeros.
Note: The value always includes a leading sign (zero is '+0'), indicating hours ahead of UTC time (+) or hours behind UTC time (-). The offset takes Daylight Savings Time into account.
-08:00
:The invariant culture time separator defined in DateTimeFormatInfo.TimeSeparator .:
/The invariant culture date separator defined in the DateTimeFormatInfo.DateSeparator ./
%cc represents a single custom format character. Produces the custom format pattern associated with the format character c. The %c specifier provides a mechanism for specifying a single custom format character and having it recognized as a custom specifier. This format is intended for characters that define both a custom and a standard format. Note that a format string containing exactly one such character will be interpreted as a standard format specifier unless prefaced with the %.
Note: For example, for the invariant culture, "%d" produces the single or double digit date, while "d" produces the date in "MM/dd/yyyy" format. Without the %, a format string containing one character would have to include leading or trailing white space to be interpreted as a custom specifier because custom formats are required to have two or more characters.
"%y" produces a two digit year without a leading zero, and not the "MMMM, yyyy" pattern.
\cc represents any character predefined as part of a format specifier. Prevents the character from being interpreted as a format specifier (the character is treated as a literal).
Note: In programming languages where the backslash ('\') character is used to specify control sequences such as newline (\n), the backslash character is required to be specified twice. For example, in C#, "\d" is coded as "\\d".
"\d" produces the character 'd', and not the day of the month.
'xx' or "xx"xx represents a string of characters of any length. The characters are treated as literals."'d'" produces the character 'd', and not the day of the month.
[Edit]

Requirements

Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0