TimeSpan.ToString Method

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

Converts the value of the current TimeSpan object to its equivalent string representation.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Overrides Function ToString As String
public override string ToString()

Return Value

Type: System.String
The string representation of the current TimeSpan value.

Remarks

The returned string is formatted with the "c" format specifier and has the following format:

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

Elements in square brackets ([ and ]) may not be included in the returned string. Colons and periods (: and.) are literal characters. The non-literal elements are listed in the following table. Note that the string returned by the ToString() method is not culture-sensitive.

Item

Description

"-"

A minus sign, which indicates a negative time interval. No sign is included for a positive time span.

"d"

The number of days in the time interval. This element is omitted if the time interval is less than one day.

"hh"

The number of hours in the time interval, ranging from 0 to 23.

"mm"

The number of minutes in the time interval, ranging from 0 to 59.

"ss"

The number of seconds in the time interval, ranging from 0 to 59.

"fffffff"

Fractional seconds in the time interval. This element is omitted if the time interval does not include fractional seconds. If present, fractional seconds are always expressed using seven decimal digits.

NoteNote:

For more information about comparing the string representation of TimeSpan and Oracle data types, see Knowledge Base article 324577: System.TimeSpan Does Not Match Oracle 9i INTERVAL DAY TO SECOND Data Type.

Notes to Callers

Support for formatting TimeSpan values was added in the Silverlight 4. However, the ToString() method overload remains culture-insensitive. Its behavior remains unchanged from previous versions of the .NET Framework for Silverlight.

Examples

The following example displays the strings returned by calling the ToString method with a number of TimeSpan values.

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim span As TimeSpan

      ' Initialize a time span to zero.
      span = TimeSpan.Zero
      outputBlock.Text &= span.ToString() & vbCrLf

      ' Initialize a time span to 14 days.
      span = New TimeSpan(-14, 0, 0, 0, 0)
      outputBlock.Text &= span.ToString() & vbCrLf

      ' Initialize a time span to 1:02:03.
      span = New TimeSpan(1, 2, 3)
      outputBlock.Text &= span.ToString() & vbCrLf


      ' Initialize a time span to 250 milliseconds.
      span = New TimeSpan(0, 0, 0, 0, 250)
      outputBlock.Text &= span.ToString() & vbCrLf

      ' Initalize a time span to 99 days, 23 hours, 59 minutes, and 59.9999999 seconds.
      span = New TimeSpan(99, 23, 59, 59, 999)
      outputBlock.Text &= span.ToString() & vbCrLf

      ' Initalize a time span to 3 hours.
      span = New TimeSpan(3, 0, 0)
      outputBlock.Text &= span.ToString() & vbCrLf

      ' Initalize a timespan to 25 milliseconds.
      span = New TimeSpan(0, 0, 0, 0, 25)
      outputBlock.Text &= span.ToString() & vbCrLf
   End Sub
End Module
' The example displays the following output:
'       00:00:00
'       -14.00:00:00
'       01:02:03
'       00:00:00.2500000
'       99.23:59:59.9990000
'       03:00:00
'       00:00:00.0250000
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      TimeSpan span;

      // Initialize a time span to zero.
      span = TimeSpan.Zero;
      outputBlock.Text += span.ToString() + "\n";

      // Initialize a time span to 14 days.
      span = new TimeSpan(-14, 0, 0, 0, 0);
      outputBlock.Text += span.ToString() + "\n";

      // Initialize a time span to 1:02:03.
      span = new TimeSpan(1, 2, 3);
      outputBlock.Text += span.ToString() + "\n";


      // Initialize a time span to 250 milliseconds.
      span = new TimeSpan(0, 0, 0, 0, 250);
      outputBlock.Text += span.ToString() + "\n";

      // Initalize a time span to 99 days, 23 hours, 59 minutes, and 59.999 seconds.
      span = new TimeSpan(99, 23, 59, 59, 999);
      outputBlock.Text += span.ToString() + "\n";

      // Initalize a time span to 3 hours.
      span = new TimeSpan(3, 0, 0);
      outputBlock.Text += span.ToString() + "\n";

      // Initalize a timespan to 25 milliseconds.
      span = new TimeSpan(0, 0, 0, 0, 25);
      outputBlock.Text += span.ToString() + "\n";
   }
}
// The example displays the following output:
//       00:00:00
//       -14.00:00:00
//       01:02:03
//       00:00:00.2500000
//       99.23:59:59.9990000
//       03:00:00
//       00:00:00.0250000

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.