Share via


TimeSpan.ToString Method (String)

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 by using the specified format.

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

Syntax

'Declaration
Public Function ToString ( _
    format As String _
) As String
public string ToString(
    string format
)

Parameters

Return Value

Type: System.String
The string representation of the current TimeSpan value in the format specified by the format parameter.

Exceptions

Exception Condition
FormatException

The format parameter is not recognized or is not supported.

Remarks

The format parameter can be any valid standard or custom format specifier for TimeSpan values. If format is equal to String.Empty or is nulla null reference (Nothing in Visual Basic), the return value of the current TimeSpan object is formatted with the common format specifier ("c"). If format is any other value, the method throws a FormatException.

The format of the returned string is defined by the formatting conventions of the current culture.

The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:

Examples

The following example uses standard TimeSpan format strings to display the string representation of each element in an array of TimeSpan values

Module Example
   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      Dim spans() As TimeSpan = { TimeSpan.Zero, New TimeSpan(-14, 0, 0, 0, 0), 
                                  New TimeSpan(1, 2, 3), 
                                  New TimeSpan(0, 0, 0, 0, 250), 
                                  New TimeSpan(99, 23, 59, 59, 999),
                                  New TimeSpan(3, 0, 0), 
                                  New TimeSpan(0, 0, 0, 0, 25) }
      Dim fmts() As String = {"c", "g", "G"}
      For Each span As TimeSpan In spans
         For Each fmt As String In fmts
            outputBlock.Text += String.Format("{0}: {1}", fmt, span.ToString(fmt)) & vbCrLf
         Next
         outputBlock.Text &= vbCrLf
      Next
   End Sub
End Module
' The example displays the following output:
'       c: 00:00:00
'       g: 0:00:00
'       G: 0:00:00:00.0000000
'       
'       c: -14.00:00:00
'       g: -14:0:00:00
'       G: -14:00:00:00.0000000
'       
'       c: 01:02:03
'       g: 1:02:03
'       G: 0:01:02:03.0000000
'       
'       c: 00:00:00.2500000
'       g: 0:00:00.25
'       G: 0:00:00:00.2500000
'       
'       c: 99.23:59:59.9990000
'       g: 99:23:59:59.999
'       G: 99:23:59:59.9990000
'       
'       c: 03:00:00
'       g: 3:00:00
'       G: 0:03:00:00.0000000
'       
'       c: 00:00:00.0250000
'       g: 0:00:00.025
'       G: 0:00:00:00.0250000
using System;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      TimeSpan[] spans = { TimeSpan.Zero, new TimeSpan(-14, 0, 0, 0, 0), 
                           new TimeSpan(1, 2, 3), 
                           new TimeSpan(0, 0, 0, 0, 250), 
                           new TimeSpan(99, 23, 59, 59, 999),
                           new TimeSpan(3, 0, 0), 
                           new TimeSpan(0, 0, 0, 0, 25) };
      string[] fmts = { "c", "g", "G" };
      foreach (TimeSpan span in spans)
      {
         foreach (string fmt in fmts)
            outputBlock.Text += String.Format("{0}: {1}", fmt, span.ToString(fmt)) + "\n";

         outputBlock.Text += "\n";
      }
   }
}
// The example displays the following output:
//       c: 00:00:00
//       g: 0:00:00
//       G: 0:00:00:00.0000000
//       
//       c: -14.00:00:00
//       g: -14:0:00:00
//       G: -14:00:00:00.0000000
//       
//       c: 01:02:03
//       g: 1:02:03
//       G: 0:01:02:03.0000000
//       
//       c: 00:00:00.2500000
//       g: 0:00:00.25
//       G: 0:00:00:00.2500000
//       
//       c: 99.23:59:59.9990000
//       g: 99:23:59:59.999
//       G: 99:23:59:59.9990000
//       
//       c: 03:00:00
//       g: 3:00:00
//       G: 0:03:00:00.0000000
//       
//       c: 00:00:00.0250000
//       g: 0:00:00.025
//       G: 0:00:00:00.0250000

Version Information

Silverlight

Supported in: 5, 4

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1

Platforms

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