[This is preliminary documentation and is subject to change.]
Assembly: Tools (in Tools.dll) Version: 1.5.2.0
Syntax
| C# |
|---|
public string ToString( string format ) |
| Visual Basic (Declaration) |
|---|
Public Function ToString ( _ format As String _ ) As String |
| Visual C++ |
|---|
public: String^ ToString( String^ format ) |
| J# |
|---|
public String ToString( String format ) |
| JScript |
|---|
public function ToString( format : String ) : String |
Parameters
- format
- Type: System..::.String
The String specifying the format to use.-or- null to use the default format defined for the type of the TimeSpanFormattable.
Return Value
A String containing the value of the current instance in the specified format.
Remarks
TimeSpanFormattable provides large pallete of formats that can be used to represent current instance as String. Predefined and custom formats can be used.
| Predefined format string | Treated as this custom format string |
|---|---|
| g (short with full hours) | -h(00):mm |
| G (long with full hours) | -h(00):mm:ss (This is default format used when no format string is specified) |
| t (short time pattern) | -hh:mm |
| T (long time pattern) | -hh:mm:ss |
| l (shortest possible from hours to milliseconds) | -((h<>0)h(0):mm:ss|(m<>0)m(0):ss|s(0))((ll<>0).lll) |
| L (shortest possible from days to milliseconds) | -((d<>0)d.)((h<>0)hh:mm:ss|(m<>0)m(0):ss|s(0))((ll<>0).lll) |
| s (shortest possible from hours to seconds) | -((h<>0)h(0):mm:ss|(m<>0)m(0):ss|s(0)) |
| S (shortest possible from days to seconds) | -((d<>0)d.)((h<>0)hh:mm:ss|(m<>0)m(0):ss|s(0)) |
| Format string | Description |
|---|---|
| d, dd, ddd, ... | Any number of lowercase ds represents days (value of Days). Number of ds determines minimal number of digits that will represent number of days. |
| d(), dd() | Custom-formated days. Integral part of TotalDays which equals to Days fromated by format specified in braces. For more information about formats in braces see below. |
| D() | Custom-formated fractional days. Value of TotalDays fromated with format specified in braces. For more information about formats in braces see below. |
| [d], [dd], [ddd], ... or [d()] or [D()] | Optional days. Same formats as described above but output is ommited when number of whole days (Days) is zero. |
| h | Short hours. Value of Hours in 24 hour format from range 0÷23 as 1 or 2 digits. |
| hh | Long hours. Value of Hours in 24 hour format from range 0÷23 always as 2 digits. |
| h() | Custom-formated hours. Integral part of TotalHours formated with format specified in braces. For more information about formats in braces see below. |
| hh() | Custom-formated hours part. Hours formated with format specified in braces. For more information about formats in braces see below. |
| H() | Custom-formated fractional hours. Value of TotalHours formated with format specified in braces. For more information about formats in braces see below. |
| m | Short minutes. Value of Minutes from range 0÷59 as 1 or 2 digits. |
| mm | Long minutes. Value of Minutes from range 0÷59 always as 2 digits. |
| m() | Custom-formated minutes. Integral part of TotalMinutes formated with format specified in braces. For more information about formats in braces see below. |
| mm() | Custom-formated whole minutes. Minutes formated with format specified in braces. For more information about formats in braces see below. |
| M() | Custom-formated fractional minutes. Value of TotalMinutes formated with formate specified in braces. For more information about formats in braces see below. |
| s | Short seconds. Value of Seconds from range 0÷59 as 1 or 2 digits. |
| ss | Long seconds. Value of Seconds from range 0÷59 always as 2 digits. |
| s() | Custom-formated seconds. Integral part of TotalSeconds formated with format specified in braces. For more information about formats in braces see below. |
| ss() | Custom-formated whole seconds. Seconds formated with format specified in braces. For more information about formats in braces see below. |
| S() | Custom-formated fractional seconds. Value of TotalSeconds fromated with format specified in braces. For more information about formats in braces see below. |
| l | Short milliseconds. Value of Milliseconds from range 0÷999 as 1, 2 or 3 digits. |
| ll | Middle milliseconds. Value of Milliseconds from range 0÷999 as 2 or 3 digits. |
| lll | Long milliseconds. Value of Milliseconds from range 0÷999 always as 3 digits. |
| l() | Custom-formated milliseconds. Integral part of TotalMilliseconds formated with format specified in braces. For more information about formats in braces see below. |
| ll() | Custom-formated whole milliseconds. TotalMilliseconds formated with format specified in braces. For more information about formats in braces see below. |
| L() | Custom-formated fractional milliseconds. Value of TotalMilliseconds formated with formate specified in braces. For more information about formats in braces see below. |
| t, tt, ttt, ... | Any number of lowercase ts represents value of Ticks. The umber of ts determines minimal number of digits copyed to output. |
| t() | Custom-formated ticks. Value of Ticks formated with format specified in braces. For more information about formats in braces see below. |
| T() | Custom-formated time. Time part represented as DateTime formated with format specified in braces. For more information about formats in braces see below. The DateTime value if made as result of addition of MinValue and current value of this TimeSpanFormattable and thus this pattern should be used only for positive TimeSpanFormattable values otherwise ArgumentOutOfRangeException may be thrown. |
| : | The time separator defined in the current TimeSeparator property that is used to differentiate hours, minutes, and seconds. |
| . | The actual character used as the decimal separator is determined by the NumberDecimalSeparator property of the NumberFormatInfo that controls formatting. |
| " | Quoted string (quotation mark). Displays the literal value of any string between two quotation marks ("). |
| ' | Quoted string (apostrophe). Displays the literal value of any string between two apostrophe (') characters. |
| %c | Represents the result associated with a custom format specifier "c", when the custom DateTime format string consists solely of that custom format specifier. This is used to determine between predefined and custom format string. Each format string that's length is 1 is treated as predefined format string. If you want to treat 1-character long custom format string as custom format string, precede it with the '%' character (otherwise it will be always treated as predefined format string even when such predefined format string doesn't exist which may lead to unexpected results or exceptions). In any other case ('%' is not first character of 2-characters long custom format string) the '%' is treated as any other unknown character and thus it is copyed to output (or causes FormatException if placed where it is not allowed). |
| \c | The escape character. 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, use two escape characters ("\\"). This described behavior takes effect in context of quoted literals (" and ') and at root level of pattern. For information how the backslash escape character is treated in format in braces see below. |
| - | Optional sign. The minus sign (defined by current culture) is copyed to output when value of this TimeSpanFormattable is negative. |
| + | Compulsory sign. The minus or plus sign (defined by current culture) is copyed to output if value of this TimeSpanFormattable is non-zero. |
| (()|()|) | Conditional formating. See below. |
| Any other character | Any unknown character is copyed to output |
Formats in braces:
There are several contexts where you can use other nested format string to format part of rendered string. Those nested format strings are also enclosed with braces (). The format string expected in braces depends on value being formated. d,h,m,s,l and t are integral numbers, D,H,M,S and L are floating point numbers and T is DateTime. You can use any format string valied for appropriate data type - predefined or custom or you can leave braces empty to use default format. Because you may want to pass closing brace ')' into custom format, you must understand how the closing brace is being esacped. If you want to pass the ')' into custom format instead of using it to close custom format block escape it with '\' (type '\)'). If you want to pass backslash ('\') into nested custom format string escape it with another '\' (type '\\'). Note if you will type any other chracter than ')' or '\' after '\' inside braces-delimited nested custom format string the whole sequence will be passed into underlying nested custom format string. For example if you want to use custom format string 'hh"(hours) and "mm"(minutes)"' (which produces something like '13(hours) and 33(minutes)' you should type
Examples
Conditional formating:
Any part of formating string can be surrounded by conditional formating construct. Sub-string in each part of conditional formatting must be valid formating string. Sub-string outside conditional formatting must be valid formating string. Conditional formatings can be nested.
Conditional formating has similar structure as if-elseif-else statement. It begins with brace '(' and ends with brace ')'. Conditions are stated in another braces '()' just at start of conditional part. 2nd and next conditional parts are delimited by pipe '|' (followsed by braces with condition). Last conditional part (the else) does not need condition. Syntax of conditional segment of formating string is:
((condition1)format1|(condition2)format2|(condition3)format3|format4)
There can be 1 or more condition-format pairs. There can be or can be not last fall-backl format. Conditions are evaluated from left to right. Format associated with firts condition that evaluates to true is emited to output. False evaluated formats and skipped formats are still parsed and must be valid. But are not emitted to output. Syntax of condition is:
value operator literalValue can be one of following:
| Value string | Meaning |
|---|---|
| d | Integral part of TotalDays |
| D | TotalDays |
| dd | Days |
| h | Integral part of TotalHours |
| H | TotalHours |
| hh | Hours |
| m | Integral part of TotalMinutes |
| M | TotalMinutes |
| mm | Minutes |
| s | Integral part of TotalSeconds |
| S | TotalSeconds |
| ss | Seconds |
| l | Integral part of TotalMilliseconds |
| L | TotalMilliseconds |
| ll | Milliseconds |
| T | TimeSpan with same value as current instance of TimeSpanFormattable |
| t | Ticks |
Any value can be surronded by pipes (||) in order to make absolute value of it.
Operator can be one of following comparison operators: <, <=, =, >, >=, <>
Literal is numeric literal in like 123.148. Can be preceded with negative sing (-). Parts on the left and on the right side of decimal dot are optinal. Examples of valid number are: 128, -128, 128., .128, 128.128, -.128, -128., -128, 128. When comparing to T (whole TimeSpan) right operand shall not be number but time value in format D.h:m:s.l. Time literal have not to be fully specified. If it is specified as number only, it is treated as hours. Days can be ommited (including first dot). Minutes, seconds and milliseconds can be ommited including leading colons (or dots). Examples of valid times are: 14 (14 hours), 14.3 (14 days, 3 hours), 14.25:10 (14 days, 25 hours, 10 minutes), 10 (10 hours), 10:00:01 (10 hours, 1 second), 0:0:0000001.321 (1.321 seconds)
Conditions does not allow any spaces in them. Any character that is not understood is treated as error. Technically you can specify multiple fall down conditions and specify |() condition after | condition. Practically no such condition is ever emitted because some of prevous conditions (|) have evaluetad to true. However syntactically it is OK, error oe not thrown. Any formating that can be used outside conditions (including so-called formats in braces) can be used inside conditions.
Note: Format string is parsed from left to right by finite deterministic state automaton and thus format string like mmm on time span of value 1:14:00 will produce "1414" etc.
Examples (both have same results):
Examples
Time is\: ((M>=60)hh:mm:ss|(S>=60)mm:ss|ss).l
Examples
Time is\: ((T>=1)hh:mm:ss|(T>=0:1)mm:ss|ss).l
Note: All numbers are rendered as absolute value, you must use +/- to display a sign
See also: Custom numeric formats, Standard numeric formats
Exceptions
| Exception | Condition |
|---|---|
| System..::.FormatException | Unknown predefined format -or- syntax error in format string |
| System..::.ArgumentOutOfRangeException | The 'T()' patter is used on negative TimeSpanFormattable or value of current TimeSpanFormattable added to MinValue causes MaxValue to be exceeded. |