Поделиться через


Standard TimeSpan Format Strings

Microsoft Silverlight will reach end of support after October 2021. Learn more.

A standard TimeSpan format string uses a single format specifier to define the text representation of a TimeSpan value that results from a formatting operation. Any format string that contains more than one character, including white space, is interpreted as a custom TimeSpan format string. For more information, see Custom TimeSpan Format Strings .

The string representations of TimeSpan values are produced by calls to the overloads of the TimeSpan.ToString method, as well as by methods that support composite formatting, such as String.Format. For more information, see Formatting Types and Composite Formatting. The following example illustrates the use of standard format strings in formatting operations.

Standard TimeSpan format strings are also used by the TimeSpan.ParseExact and TimeSpan.TryParseExact methods to define the required format of input strings for parsing operations. (Parsing converts the string representation of a value to that value.) The following example illustrates the use of standard format strings in parsing operations.

The following table lists the standard time interval format specifiers.

Format specifier

Name

Description

Examples

"c"

Constant (invariant) format

This specifier is not culture-sensitive. It takes the form [-][d’.’]hh’:’mm’:’ss[‘.’fffffff].

(The "t" and "T" format strings produce the same results.)

More information: The Constant ("c") Format Specifier.

TimeSpan.Zero -> 00:00:00

New TimeSpan(0, 0, 30, 0) -> 00:30:00

New TimeSpan(3, 17, 25, 30, 500) -> 3.17:25:30.5000000

"g"

General short format

This specifier outputs only what is needed. It is culture-sensitive and takes the form [-][d’:’]h’:’mm’:’ss[.FFFFFFF].

More information: The General Short ("g") Format Specifier.

New TimeSpan(1, 3, 16, 50, 500) -> 1:3:16:50.5 (en-US)

New TimeSpan(1, 3, 16, 50, 500) -> 1:3:16:50,5 (fr-FR)

New TimeSpan(1, 3, 16, 50, 599) -> 1:3:16:50.599 (en-US)

New TimeSpan(1, 3, 16, 50, 599) -> 1:3:16:50,599 (fr-FR)

"G"

General long format

This specifier always outputs days and seven fractional digits. It is culture-sensitive and takes the form [-]d’:’hh’:’mm’:’ss.fffffff.

More information: The General Long ("G") Format Specifier.

New TimeSpan(18, 30, 0) -> 0:18:30:00.0000000 (en-US)

New TimeSpan(18, 30, 0) -> 0:18:30:00,0000000 (fr-FR)

The Constant ("c") Format Specifier

The "c" format specifier returns the string representation of a TimeSpan value in the following form:

[-][d.]hh:mm:ss[.fffffff]

Elements in square brackets ([ and ]) are optional. The period (.) and colon (:) are literal symbols. The following table describes the remaining elements.

Element

Description

-

An optional negative sign, which indicates a negative time interval.

d

The optional number of days, with no leading zeros.

hh

The number of hours, which ranges from "00" to "23".

mm

The number of days, which ranges from "00" to "59".

ss

The number of seconds, which ranges from "0" to "59".

fffffff

The optional number of milliseconds, which ranges from "0010000" to "9990000".

Unlike the "g" and "G" format specifiers, the "c" format specifier is not culture-sensitive. It produces the string representation of a TimeSpan value that is invariant and that is common to all previous versions of the .NET Framework for Silverlight before Silverlight 4. "c" is the default TimeSpan format string; the TimeSpan.ToString() method formats a time interval value by using the "c" format string.

NoteNote:

TimeSpan also supports the "t" and "T" standard format strings, which are identical in behavior to the "c" standard format string.

The following example instantiates two TimeSpan objects, uses them to perform arithmetic operations, and displays the result. In each case, it uses composite formatting to display the TimeSpan value by using the "c" format specifier.

Back to table

The General Short ("g") Format Specifier

The "g" TimeSpan format specifier returns the string representation of a TimeSpan value in a compact form by including only the elements that are necessary. It has the following form:

[-][d:]h:mm:ss[.FFFFFFF]

Elements in square brackets ([ and ]) are optional. The colon (:) is a literal symbol. The following table describes the remaining elements.

Element

Description

-

An optional negative sign, which indicates a negative time interval.

d

The optional number of days, with no leading zeros.

h

The number of hours, which ranges from "0" to "23", with no leading zeros.

mm

The number of minutes, which ranges from "00" to "59"..

ss

The number of seconds, which ranges from "00" to "59"..

.

The fractional seconds separator. It is equivalent to the specified culture's NumberDecimalSeparator property without user overrides.

FFFFFFF

The fractional seconds. As few digits as possible are displayed.

Like the "G" format specifier, the "g" format specifier is localized. Its fractional seconds separator is based on either the current culture or a specified culture's NumberDecimalSeparator property.

The following example instantiates two TimeSpan objects, uses them to perform arithmetic operations, and displays the result. In each case, it uses composite formatting to display the TimeSpan value by using the "g" format specifier. In addition, it formats the TimeSpan value by using the formatting conventions of the current system culture (which, in this case, is English - United States or en-US) and the French - France (fr-FR) culture.

Back to table

The General Long ("G") Format Specifier

The "G" TimeSpan format specifier returns the string representation of a TimeSpan value in a long form that always includes both days and fractional seconds. The string that results from the "G" standard format specifier has the following form:

[-]d:hh:mm:ss.fffffff

Elements in square brackets ([ and ]) are optional. The colon (:) is a literal symbol. The following table describes the remaining elements.

Element

Description

-

An optional negative sign, which indicates a negative time interval.

d

The number of days, with no leading zeros.

hh

The number of hours, which ranges from "00" to "23".

mm

The number of days, which ranges from "00" to "59".

ss

The number of seconds, which ranges from "00" to "59".

.

The fractional seconds separator. It is equivalent to the specified culture's NumberDecimalSeparator property without user overrides.

fffffff

The fractional seconds.

Like the "G" format specifier, the "g" format specifier is localized. Its fractional seconds separator is based on either the current culture or a specified culture's NumberDecimalSeparator property.

The following example instantiates two TimeSpan objects, uses them to perform arithmetic operations, and displays the result. In each case, it uses composite formatting to display the TimeSpan value by using the "G" format specifier. In addition, it formats the TimeSpan value by using the formatting conventions of the current system culture (which, in this case, is English - United States or en-US) and the French - France (fr-FR) culture.

Back to table