Custom Formatting of TimeSpan Columns


If the format you want to use cannot be created with the given settings, the custom format string allows you to create your own formats using a code explained in the examples below. There are five different data values included in the TimeSpan format: days, hours, minutes, seconds and fractions of seconds.

Examples:

These can be combined to a suitable format using a format string built by the following specifier characters:

Character

Description

d

Returns the number of days.

h

Returns the number of hours with one or two digits, depending on the value. This means that six hours will be written as '6' (whereas 11 hours are written as '11') when this format string is applied.

hh

Returns the number of hours with two digits. This means that six hours will be written as '06', when this format string is applied.

m

Returns the number of minutes with one or two digits, depending on the value.

This means that six minutes will be written as '6' (whereas 11 minutes are written as '11') when this format string is applied.

mm

Returns the number of minutes with two digits. This means that six minutes will be written as '06'.

s

Returns the number of seconds with one or two digits, depending on the value.

This means that six seconds will be written as '6' (whereas 11 seconds are written as '11') when this format string is applied.

ss

Returns the number of seconds with two digits. This means that six seconds will be written as '06'.

f

Returns the fractions of seconds.

When specifying custom format strings, you can also add a number between 1 and 3 after the f, defining how many decimals are shown. If no number has been specified, three numbers are shown, if available.

Between each specifier character, you need to supply some kind of separator. This could be a custom string value, but if any of the specifier characters are included in the string, they need to be escaped by a backslash (\). You can also include an initial and a conclusive string.

All the examples below use the following value from the data: -5 days, 7 hours, 11 minutes, 3.1234 seconds.

Format string

Result

d.h:m:s.f

-5.7:11:3.1

d.hh:mm:ss.f2

-5.07:11:03.12

Ti\me\span i\s d \day\s

Timespan is -5 days

d \day\s h \hour\s m \minute\s s \secon\d\s

-5 days 7 hours 11 minutes 3 seconds

f s m h d

123 3 11 7 -5

See also:

Custom Formatting of Date and Time Columns

Custom Formatting of Numeric Columns

Formatting Overview