Format Specifiers

The following custom date and time format specifiers work with functions and methods.

Format Specifier Description
d Represents the day of the month as a number from 1 through 31. A single-digit day is formatted without a leading zero.
dd Represents the day of the month as a number from 01 through 31. A single-digit day is formatted with a leading zero. The following example includes the dd custom format specifier in a custom format string.
ddd Represents the abbreviated name of the day of the week as defined in the current DateTimeFormatInfo.AbbreviatedDayNames property. The following example includes the ddd custom format specifier in a custom format string.
dddd (plus any number of additional d specifiers) Represents the full name of the day of the week as defined in the current DateTimeFormatInfo.DayNames property. The following example includes the dddd custom format specifier in a custom format string.
f

Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value.

If the f format specifier is used without other format specifiers, it is interpreted as the f standard date and time format specifier.

When used as part of a format string supplied to the ParseExact, TryParseExact, ParseExact, or TryParseExact methods, the number of f format specifiers used indicates the number of most significant digits of the seconds fraction that must be present to successfully parse the string.

ff Represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value. The following example includes the ff custom format specifier in a custom format string.
fff Represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value. The following example includes the fff custom format specifier in a custom format string.
ffff

Represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value.

While it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

fffff

Represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value.

While it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

ffffff

Represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value.

While it is possible to display the millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

fffffff

Represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value.

While it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

F

Represents the most significant digit of the seconds fraction; that is, it represents the tenths of a second in a date and time value. Nothing is displayed if the digit is zero.

If the F format specifier is used without other format specifiers, it is interpreted as the F standard date and time format specifier.

The number of F format specifiers used with the ParseExact, TryParseExact, ParseExact, or TryParseExact methods indicates the maximum number of most significant digits of the seconds fraction that can be present to successfully parse the string.

FF Represents the two most significant digits of the seconds fraction; that is, it represents the hundredths of a second in a date and time value. However, trailing zeros or two zero digits are not displayed. The following example includes the FF custom format specifier in a custom format string.
FFF Represents the three most significant digits of the seconds fraction; that is, it represents the milliseconds in a date and time value. However, trailing zeros or three zero digits are not displayed. The following example includes the FFF custom format specifier in a custom format string.
FFFF

Represents the four most significant digits of the seconds fraction; that is, it represents the ten thousandths of a second in a date and time value. However, trailing zeros or four zero digits are not displayed.

While it is possible to display the ten thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

FFFFF

Represents the five most significant digits of the seconds fraction; that is, it represents the hundred thousandths of a second in a date and time value. However, trailing zeros or five zero digits are not displayed.

While it is possible to display the hundred thousandths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

FFFFFF

Represents the six most significant digits of the seconds fraction; that is, it represents the millionths of a second in a date and time value. However, trailing zeros or six zero digits are not displayed.

While it is possible to display the millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

FFFFFFF

Represents the seven most significant digits of the seconds fraction; that is, it represents the ten millionths of a second in a date and time value. However, trailing zeros or seven zero digits are not displayed.

While it is possible to display the ten millionths of a second component of a time value, that value may not be meaningful. The precision of date and time values depends on the resolution of the system clock. On Windows NT 3.5 and later, and Windows Vista operating systems, the clock's resolution is approximately 10-15 milliseconds.

g, gg (plus any number of additional g specifiers) Represents the period or era, for example, A.D. Formatting ignores this specifier if the date to be formatted does not have an associated period or era string.
h Represents the hour as a number from 1 through 12, that is, the hour as represented by a 12-hour clock that counts the whole hours since midnight or noon. A particular hour after midnight is indistinguishable from the same hour after noon. The hour is not rounded, and a single-digit hour is formatted without a leading zero. For example, given a time of 5:43, this custom format specifier displays "5".
hh, hh (plus any number of additional h specifiers) Represents the hour as a number from 01 through 12, that is, the hour as represented by a 12-hour clock that counts the whole hours since midnight or noon. A particular hour after midnight is indistinguishable from the same hour after noon. The hour is not rounded, and a single-digit hour is formatted with a leading zero. For example, given a time of 5:43, this format specifier displays "05".
H Represents the hour as a number from 0 through 23, that is, the hour as represented by a zero-based 24-hour clock that counts the hours since midnight. A single-digit hour is formatted without a leading zero.
HH, HH (plus any number of additional H specifiers) Represents the hour as a number from 00 through 23, that is, the hour as represented by a zero-based 24-hour clock that counts the hours since midnight. A single-digit hour is formatted with a leading zero. The following example includes the HH custom format specifier in a custom format string.
K Represents the time zone information of a date and time value. When used with DateTime values, the result string is defined by the value of the DateTime.Kind property. For the local time zone (a DateTime.Kind property value of DateTimeKind.Local), this specifier is equivalent to the zzz specifier and produces a result string containing the local offset from Coordinated Universal Time (UTC); for example, "-07:00". For a Utc time (a DateTime.Kind property value of DateTimeKind.Utc), the result string includes a "Z" character to represent a UTC date. For a time from an unspecified time zone (a time whose DateTime.Kind property equals DateTimeKind.Unspecified, the result is equivalent to String.Empty. For DateTimeOffset values, the K format specifier is equivalent to the zz format specifier, and produces a result string containing the DateTimeOffset value's offset from UTC.
m Represents the minute as a number from 0 through 59. The minute represents whole minutes that have passed since the last hour. A single-digit minute is formatted without a leading zero. The following example includes the m custom format specifier in a custom format string.
mm, mm (plus any number of additional m specifiers) Represents the minute as a number from 00 through 59. The minute represents whole minutes that have passed since the last hour. A single-digit minute is formatted with a leading zero. The following example includes the mm custom format specifier in a custom format string.
M Represents the month as a number from 1 through 12. A single-digit month is formatted without a leading zero. The following example includes the M custom format specifier in a custom format string.
MM Represents the month as a number from 01 through 12. A single-digit month is formatted with a leading zero. The following example includes the MM custom format specifier in a custom format string.
MMM Represents the abbreviated name of the month as defined in the current DateTimeFormatInfo.AbbreviatedMonthNames property. The following example includes the MMM custom format specifier in a custom format string.
MMMM Represents the full name of the month as defined in the current DateTimeFormatInfo.MonthNames property. The following example includes the MMMM custom format specifier in a custom format string.
s Represents the seconds as a number from 0 through 59. The result represents whole seconds that have passed since the last minute. A single-digit second is formatted without a leading zero. The following example includes the g custom format specifier in a custom format string.
ss, ss (plus any number of additional s specifiers) Represents the seconds as a number from 00 through 59. The result represents whole seconds that have passed since the last minute. A single-digit second is formatted with a leading zero. The following example includes the ss custom format specifier in a custom format string.
t Represents the first character of the AM/PM designator defined in the current DateTimeFormatInfo.AMDesignator or DateTimeFormatInfo.PMDesignator property. The AM designator is used if the hour in the time being formatted is less than 12. Otherwise, the PM designator is used.
tt, tt (plus any number of additional t specifiers)

Represents the AM/PM designator as defined in the current DateTimeFormatInfo.AMDesignator or DateTimeFormatInfo.PMDesignator property. The AM designator is used if the hour in the time being formatted is less than 12. Otherwise, the PM designator is used.

Your application should be sure to use the tt specifier for languages for which it is necessary to maintain the distinction between AM and PM. An example is Japanese, for which the AM and PM designators differ in the second character instead of the first character.

y Represents the year as a one or two-digit number. If the year has more than two digits, only the two low-order digits appear in the result. If the first digit of a two-digit year begins with a zero (for example, 2008), the number is formatted without a leading zero.
yy Represents the year as a two-digit number. If the year has more than two digits, only the two low-order digits appear in the result. If the two-digit year has fewer than two significant digits, the number is padded with leading zeros to achieve two digits.
yyy

Represents the year with a minimum of three digits. If the year has more than three significant digits, they are included in the result string. If the year has fewer than three digits, the number is padded with leading zeros to achieve three digits.

For the Thai Buddhist calendar, which can have five-digit years, this format specifier displays all five digits.

yyyy

Represents the year as a four-digit number. If the year has more than four digits, only the four low-order digits appear in the result. If the year has fewer than four digits, the number is padded with leading zeros to achieve four digits.

For the Thai Buddhist calendar, which can have five-digit years, this format specifier includes all five digits.

yyyyy (plus any number of additional y specifiers)

Represents the year as a five-digit number. If the year has more than five digits, only the five low-order digits appear in the result. If the year has fewer than five digits, the number is padded with leading zeroes to achieve five digits.

If there are additional y specifiers, the number is padded with as many leading zeroes as necessary to achieve the number of y specifiers.

z

With DateTime values, represents the signed offset of the local operating system's time zone from Coordinated Universal Time (UTC), measured in hours. It does not reflect the value of an instance's Kind property. For this reason, the z format specifier is not recommended for use with DateTime values.

With DateTimeOffset values, represents the DateTimeOffset value's offset from UTC in hours.

The offset is always displayed with a leading sign. A plus sign (+) indicates hours ahead of and a minus sign (-) indicates hours behind UTC. The offset ranges from –12 through +13. A single-digit offset is formatted without a leading zero.

zz

With DateTime values, represents the signed offset of the local operating system's time zone from UTC, measured in hours. It does not reflect the value of an instance's Kind property. For this reason, the zz format specifier is not recommended for use with DateTime values.

With DateTimeOffset values, represents the DateTimeOffset value's offset from UTC in hours.

The offset is always displayed with a leading sign. A plus sign (+) indicates hours ahead of and a minus sign (-) indicates hours behind UTC. The offset ranges from –12 through +13. A single-digit offset is formatted with a leading zero.

zzz, zzz (plus any number of additional z specifiers)

With DateTime values, represents the signed offset of the local operating system's time zone from UTC, measured in hours and minutes. It does not reflect the value of an instance's Kind property. For this reason, the zzz format specifier is not recommended for use with DateTime values.

With DateTimeOffset values, represents the DateTimeOffset value's offset from UTC in hours and minutes.

The offset is always displayed with a leading sign. A plus sign (+) indicates hours ahead of and a minus sign (-) indicates hours behind UTC. The offset ranges from –12 through +13. A single-digit offset is formatted with a leading zero.

: Represents the time separator defined in the current DateTimeFormatInfo.TimeSeparator property. This separator is used to differentiate hours, minutes, and seconds.
/ Represents the date separator defined in the current DateTimeFormatInfo.DateSeparator property. This separator is used to differentiate years, months, and days.
" Represents a quoted string (quotation mark). Displays the literal value of any string between two quotation marks ("). Your application should precede each quotation mark with an escape character (\).
' Represents a quoted string (apostrophe). Displays the literal value of any string between two apostrophe (') characters.
%c

Represents the result associated with a c custom format specifier, when the custom date and time format string consists solely of that custom format specifier.

That is, to use the d, f, F, h, m, s, t, y, z, H, or M custom format specifier by itself, the application should specify %d, %f, %F, %h, %m, %s, %t, %y, %z, %H, or %M.

\c Represents the escape character, and displays the character "c" as a literal when that character is preceded by the escape character (\). To insert the backslash character itself in the result string, the application should use two escape characters ("\\").
Any other character Copies any other character to the result string, without affecting formatting.

 

Numeric Format Strings

Format Specifier Description
C or c

Currency

The number is converted to a string that represents a currency amount. The conversion is controlled by the currency format information of the current NumberFormatInfo object.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default currency precision given by the current NumberFormatInfo object is used.

D or d

Decimal

This format is supported only for integral types. The number is converted to a string of decimal digits (0-9), prefixed by a minus sign if the number is negative.

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.

E or e

Scientific (exponential)

The number is converted to a string of the form "-d.ddd…E+ddd" or "-d.ddd…e+ddd", where each 'd' indicates a digit (0-9). The string starts with a minus sign if the number is negative. One digit always precedes the decimal point.

The precision specifier indicates the desired number of digits after the decimal point. If the precision specifier is omitted, a default of six digits after the decimal point is used.

The case of the format specifier indicates whether to prefix the exponent with an 'E' or an 'e'. The exponent always consists of a plus or minus sign and a minimum of three digits. The exponent is padded with zeros to meet this minimum, if required.

F or f

Fixed-point

The number is converted to a string of the form "-ddd.ddd…" where each 'd' indicates a digit (0-9). The string starts with a minus sign if the number is negative.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision is given by the NumberDecimalDigits property of the current NumberFormatInfo object.

G or g

General

The number is converted to the most compact of either fixed-point or scientific notation, depending on the type of the number and whether a precision specifier is present. If the precision specifier is omitted or zero, the type of the number determines the default precision, as indicated by the following list.

Byte or SByte: 3

Int16 or UInt16: 5

Int32 or UInt32: 10

Int64 or UInt64: 19

Single: 7

Double: 15

Decimal: 29

Fixed-point notation is used if the exponent that would result from expressing the number in scientific notation is greater than -5 and less than the precision specifier; otherwise, scientific notation is used. The result contains a decimal point if required and trailing zeroes are omitted. If the precision specifier is present and the number of significant digits in the result exceeds the specified precision, then the excess trailing digits are removed by rounding.

The exception to the preceding rule is if the number is a Decimal and the precision specifier is omitted. In that case, fixed-point notation is always used and trailing zeroes are preserved.

If scientific notation is used, the exponent in the result is prefixed with 'E' if the format specifier is 'G', or 'e' if the format specifier is 'g'.

N or n

Number

The number is converted to a string of the form "-d,ddd,ddd.ddd…", where '-' indicates a negative number symbol if required, 'd' indicates a digit (0-9), ',' indicates a thousand separator between number groups, and '.' indicates a decimal point symbol. The actual negative number pattern, number group size, thousand separator, and decimal separator are specified by the NumberNegativePattern, NumberGroupSizes, NumberGroupSeparator, and NumberDecimalSeparator properties, respectively, of the current NumberFormatInfo object.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision is given by the NumberDecimalDigits property of the current NumberFormatInfo object.

P or p

Percent

The number is converted to a string that represents a percent as defined by the NumberFormatInfo.PercentNegativePattern property if the number is negative, or the NumberFormatInfo.PercentPositivePattern property if the number is positive. The converted number is multiplied by 100 in order to be presented as a percentage.

The precision specifier indicates the desired number of decimal places. If the precision specifier is omitted, the default numeric precision given by the current NumberFormatInfo object is used.

R or r

Round-trip

This format is supported only for the Single and Double types. The round-trip specifier guarantees that a numeric value converted to a string will be parsed back into the same numeric value. When a numeric value is formatted using this specifier, it is first tested using the general format, with 15 spaces of precision for a Double and 7 spaces of precision for a Single. If the value is successfully parsed back to the same numeric value, it is formatted using the general format specifier. However, if the value is not successfully parsed back to the same numeric value, then the value is formatted using 17 digits of precision for a Double and 9 digits of precision for a Single.

Although a precision specifier can be present, it is ignored. Round trips are given precedence over precision when using this specifier.

X or x

Hexadecimal

This format is supported only for integral types. The number is converted to a string of hexadecimal digits. The case of the format specifier indicates whether to use uppercase or lowercase characters for the hexadecimal digits greater than 9. For example, use 'X' to produce "ABCDEF", and 'x' to produce "abcdef".

The precision specifier indicates the minimum number of digits desired in the resulting string. If required, the number is padded with zeros to its left to produce the number of digits given by the precision specifier.

0

Zero placeholder

If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the result string; otherwise, a '0' appears in the result string. The position of the leftmost '0' before the decimal point and the rightmost '0' after the decimal point determines the range of digits that are always present in the result string.

The "00" specifier causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used.

For example, formatting 34.5 with "00" would result in the value 35. The following example displays several values formatted using custom format strings that include zero placeholders.

#

Digit placeholder

If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the result string. Otherwise, nothing is stored in that position in the result string.

This specifier never displays the '0' character if it is not a significant digit, even if '0' is the only digit in the string. It will display the '0' character if it is a significant digit in the number being displayed.

The "##" format string causes the value to be rounded to the nearest digit preceding the decimal, where rounding away from zero is always used.

For example, formatting 34.5 with "##" would result in the value 35. The following example displays several values formatted using custom format strings that include digit placeholders.

.

Decimal point

The first '.' character in the format string determines the location of the decimal separator in the formatted value; any additional '.' characters are ignored.

The actual character used as the decimal separator in the result string is determined by the NumberDecimalSeparator property of the NumberFormatInfo object that controls formatting.

,

Thousand separator and number scaling

The ',' character serves as both a thousand separator specifier and a number scaling specifier.

Thousand separator specifier: If one or more ',' characters is specified between two digit placeholders (0 or #) that format the integral digits of a number, a group separator character is inserted between each number group in the integral part of the output.

The NumberGroupSeparator and NumberGroupSizes properties of the current NumberFormatInfo object determine the character used as the number group separator and the size of each number group. For example, if the string "#,#" and the invariant culture are used to format the number 1000, the output is "1,000".

Number scaling specifier: If one or more ',' characters is specified immediately to the left of the explicit or implicit decimal point, the number to be formatted is divided by 1000 each time a number scaling specifier occurs. For example, if the string "0,," is used to format the number 100 million, the output is "100".

You can use thousand separator and number scaling specifiers in the same format string. For example, if the string "#,0,," and the invariant culture are used to format the number one billion, the output is "1,000".

%

Percentage placeholder

The presence of a '%' character in a format string causes a number to be multiplied by 100 before it is formatted. The appropriate symbol is inserted in the number itself at the location where the '%' appears in the format string.

The percent character used is dependent on the current NumberFormatInfo class.

E0

E+0

E-0

e0

e+0

e-0

Scientific notation

If any of the strings "E", "E+", "E-", "e", "e+", or "e-" are present in the format string and are followed immediately by at least one '0' character, then the number is formatted using scientific notation with an 'E' or 'e' inserted between the number and the exponent.

The number of '0' characters following the scientific notation indicator determines the minimum number of digits to output for the exponent. The "E+" and "e+" formats indicate that a sign character (plus or minus) should always precede the exponent. The "E", "E-", "e", or "e-" formats indicate that a sign character should only precede negative exponents.

\

Escape character

In C# and C++, the backslash character causes the next character in the format string to be interpreted as an escape sequence. It is used with traditional formatting sequences like '\n' (new line).

In some languages, the escape character itself must be preceded by an escape character when used as a literal.

Otherwise, the compiler interprets the character as an escape sequence.

Use the string "\\" to display '\'.

This escape character is not supported in Visual Basic; however, ControlChars provides the same functionality.

"abc"

'abc'

Literal string

Characters enclosed in single or double quotes are copied to the result string, and do not affect formatting.

;

Section separator

The ';' character is used to separate sections for positive, negative, and zero numbers in the format string.

If there are two sections in the custom format string, the leftmost section defines the formatting of positive and zero numbers, while the rightmost section defines the formatting of negative numbers.

If there are three sections, the leftmost section defines the formatting of positive numbers, the middle section defines the formatting of zero numbers, and the rightmost section defines the formatting of negative numbers.

Other

All other characters

Any other character is copied to the result string, and does not affect formatting.