Wednesday, March 27, 2013

.Net Format strings Cheatsheet


Microsoft.Net Format Strings:

What are format strings.. Format strings are used to represent data in a specific format for display purposes or readability purpose. Every type (DateTime, string, Number) in .net type system has it's own set of format strings which customize the output in various ways.  Personally, I often look around for easy and quick format strings to format dates, strings. I wish I had cheat sheet for quick reference. Following cheat sheet is created by benevolent guy called John sheehan. I'm just sharing his work for all poor souls including myself.


.NET Standard DateTime Format Strings

Specifier
Name
Description




d

Short date pattern
Represents a custom DateTime format string defined by the current ShortDatePattern property.




D

Long date pattern
Represents a custom DateTime format string defined by the current LongDatePattern property.




f

Full date/time pattern
Represents a combination of the long date (D) and short time (t) patterns, separated by a space.


(short time)





F

Full date/time pattern
Represents a custom DateTime format string defined by the current FullDateTimePattern property.


(long time)





g

General date/time
Represents a combination of the short date (d) and short time (t) patterns, separated by a space.


pattern (short time)





G

General date/time
Represents a combination of the short date (d) and long time (T) patterns, separated by a space.


pattern (long time)





or
m
Month day pattern
Represents a custom DateTime format string defined by the current MonthDayPattern property.




o

Round-trip date/time
Represents a custom DateTime format string using a pattern that preserves time zone information. The pattern is designed to round-trip DateTime


pattern
formats, including the Kind property, in text. Then the formatted string can be parsed back using Parse or ParseExact with the correct Kind property



value. Equivalent custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss.fffffffK".




or
r
RFC1123 pattern
Represents a custom DateTime format string defined by the current RFC1123Pattern property. The pattern is a defined standard and the property is read-



only. Equivalent custom format string is "ddd, dd MMM yyyy HH':'mm':'ss 'GMT'". Does not convert DateTimes to UTC.




s

Sortable date/time
Represents a custom DateTime format string defined by the current SortableDateTimePattern property.


pattern; ISO 8601
This pattern is a defined standard and the property is read-only. Equivalent custom format string is "yyyy'-'MM'-'dd'T'HH':'mm':'ss".




t

Short time pattern
Represents a custom DateTime format string defined by the current ShortTimePattern property.



For example, the custom format string for the invariant culture is "HH:mm".




T

Long time pattern
Represents a custom DateTime format string defined by the current LongTimePattern property.



For example, the custom format string for the invariant culture is "HH:mm:ss".




u

Universal sortable
Represents a custom DateTime format string defined by the current UniversalSortableDateTimePattern property.


date/time pattern
Equivalent custom format string is "yyyy'-'MM'-'dd HH':'mm':'ss'Z'". Does not convert DateTimes to UTC.




U

Universal sortable
Represents a custom DateTime format string defined by the current FullDateTimePattern property. This pattern is the same as the full date/long time (F)


date/time pattern
pattern. However, formatting operates on the Coordinated Universal Time (UTC) that is equivalent to the DateTime object being formatted.




or
y
Year month pattern
Represents a custom DateTime format string defined by the current YearMonthPattern property.



For example, the custom format string for the invariant culture is "yyyy MMMM".



Any other single
(Unknown specifier)
An unknown specifier throws a runtime format exception.
character












.NET Custom DateTime Format Strings


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.


ddd
Represents the abbreviated name of the day of the week as defined in the current System.Globalization.DateTimeFormatInfo.AbbreviatedDayNames property.


dddd
Represents the full name of the day of the week as defined in the current System.Globalization.DateTimeFormatInfo.DayNames property.


f                 Represents the most significant digit of the seconds fraction.

Note that if the "f" format specifier is used alone, without other format specifiers, it is interpreted as the "f" standard DateTime format specifier (full date/time pattern).

When you use this format specifier with the ParseExact or TryParseExact method, the number of "f" format specifiers that you use indicates the number of most significant digits of the seconds fraction to parse.
ff…
Number of repeated specifiers represents most significant digits of the seconds fraction.



F                 Represents the most significant digit of the seconds fraction. Nothing is displayed if the digit is zero.

When you use this format specifier with the ParseExact or TryParseExact method, the number of "F" format specifiers that you use indicates the maximum number of most significant digits of the seconds fraction to parse.
FF…

Number of repeated specifiers represents most significant digits of the seconds fraction. Trailing zeros, or two zero digits, are not displayed.



g or
gg
Represents the period or era (A.D. for example). This specifier is ignored 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 single-digit hour is formatted without a leading zero.

hh                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 single-digit hour is formatted with a leading zero.

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                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.

K                   Represents different values of the DateTime.Kind property, that is, Local, Utc, or Unspecified. This specifier round-trips the kind value in text and preserves the time zone. For the Local kind value, this specifier is equivalent to the "zzz" specifier and displays the local offset, for example, "-07:00". For the Utc kind value, the specifier displays a "Z" character to represent a UTC date. For the Unspecified kind value, the specifier is equivalent to "" (nothing).

m                 Represents the minute as a number from 0 through    59. The minute represents whole minutes passed since the last hour. A single-digit minute is formatted without a leading zero.

mm                Represents the minute as a number from 00 through 59. The minute represents whole minutes passed since the last hour. A single-digit minute is formatted with a leading zero.






















.NET Standard Number Format Strings

Specifier
Name
Description




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. Precision specifier (eg. “{0:C5}” allowed.




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. Precision



specifier (eg. “{0:d3}” allowed.




or
e
Scientific
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


(exponential)
number is negative. One digit always precedes the decimal point.



Precision specifier (eg. “{0:E5}” allowed. 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.




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.



Precision specifier (eg. “{0:f4}” allowed.




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.




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 current NumberFormatInfo object. Precision specifier (eg. “{0:N5}” allowed.




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.



Precision specifier (eg. “{0:p6}” allowed.




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. Precision specifier NOT allowed.




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. Precision specifier (eg. "{0:x4}" allowed. If required, the number is padded with zeros to its



left to produce the number of digits given by the precision specifier.



Any other
(Unknown
An unknown specifier throws a runtime format exception.
single char.
specifier)



More .NET Cheat Sheets available at http://john-sheehan.com/blog/