See Also: DateTimeFormatInfo Members
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.
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 Specifier | Format Pattern | Description |
|---|---|---|
| d | MM/dd/yyyy | The full date in numeric format (DateTimeFormatInfo.ShortDatePattern ). |
| D | dddd*, dd MMMM* yyyy | The full date including the day of the week and the name of the month (DateTimeFormatInfo.LongDatePattern ). |
| f | dddd*, 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 ). |
| F | dddd*, dd MMMM* yyyy HH*:mm*:ss* | The full date and time, including the seconds (DateTimeFormatInfo.FullDateTimePattern equivalent to DateTimeFormatInfo.LongDatePattern combined with DateTimeFormatInfo.LongTimePattern ). |
| g | MM/dd/yyyy HH*:mm* | A general date pattern including the short time form (DateTimeFormatInfo.ShortDatePattern combined with DateTimeFormatInfo.ShortTimePattern ). |
| G | MM/dd/yyyy HH*:mm*:ss* | A general date pattern including the long time form (DateTimeFormatInfo.ShortDatePattern combined with DateTimeFormatInfo.LongTimePattern ). |
| m, M | MMMM* dd | The full name of the month and the date (DateTimeFormatInfo.MonthDayPattern ). |
| t | HH*:mm* | The time in short format (DateTimeFormatInfo.ShortTimePattern ). |
| T | HH*:mm*:ss* | The time in long format (DateTimeFormatInfo.LongTimePattern ). |
| U | dddd*, dd MMMM* yyyy HH*:mm*:ss* | The full date and time, including the seconds, in the Gregorian calendar ( DateTimeFormatInfo.FullDateTimePattern ). |
| y, Y | yyyy MMMM* | The full name of the month and the year in four-digit format (DateTimeFormatInfo.YearMonthPattern ). |
| Format Pattern | Description | Examples |
|---|---|---|
| d | The day of the month as a value in the range 1-31, inclusive. Single-digit days do not have a leading zero. | 1 22 |
| dd | The day of the month as a value in the range 1-31, inclusive. Single-digit days have a leading zero. | 01 22 |
| ddd | The 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 |
| M | The numeric month as a value in the range 1-12, inclusive. Single-digit months do not have a leading zero. | 2 11 |
| MM | The numeric month as a value in the range 1-12, inclusive. Single-digit months have a leading zero. | 02 11 |
| MMM | The abbreviated name of the month, as defined in DateTimeFormatInfo.AbbreviatedMonthNames . | Feb |
| MMMM* | The full name of the month, as defined in DateTimeFormatInfo.MonthNames . | February |
| y | The year without the century (two-digit). If the value is less than 10, the year is displayed with no leading zero. | 0 3 |
| yy | The 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 |
| yyyy | The 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. |
| h | The 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 |
| H | The 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 |
| m | The 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 |
| s | The
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 |
| t | The
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 |
| z | The 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 |
| zz | The 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 . | / |
| %c | c 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. |
| \c | c 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. |