Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TimeSpan.ToString Method

Returns the string representation of the value of this instance.

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

public override string ToString ()
public String ToString ()
public override function ToString () : String
Not applicable.

Return Value

A string that represents the value of this instance. The return value is of the form: [-][d.]hh:mm:ss[.ff] Items in square brackets ([ and ]) are optional, colons and periods (: and.) are literal characters; and the other items are as follows.

Item

Description

"-"

optional minus sign indicating a negative time

"d"

optional days

"hh"

hours, ranging from 0 to 23

"mm"

minutes, ranging from 0 to 59

"ss"

seconds, ranging from 0 to 59

"ff"

optional fractional seconds, from 1 to 7 decimal digits

NoteNote:

For more information about comparing the string representation of TimeSpan and Oracle data types, see article Q324577, "System.TimeSpan Does Not Match Oracle 9i INTERVAL DAY TO SECOND Data Type," in the Microsoft Knowledge Base at http://support.microsoft.com.

The return value of this method can be consumed by Parse.

The following code example creates TimeSpan strings from TimeSpan objects using the ToString method.

// Example of the TimeSpan.Parse( string ) and TimeSpan.ToString( ) 
// methods.
using System;

class TSParseToStringDemo
{
    static void ParseNDisplayTimeSpan( string intervalStr )
    {
        // Write the first part of the output line.
        Console.Write( "{0,20}   ", intervalStr );

        // Parse the parameter, and then convert it back to a string.
        try
        {
            TimeSpan intervalVal = TimeSpan.Parse( intervalStr );
            string   intervalToStr = intervalVal.ToString( );

            // Pad the end of the TimeSpan string with spaces if it 
            // does not contain milliseconds.
            int pIndex = intervalToStr.IndexOf( ':' );
            pIndex = intervalToStr.IndexOf( '.', pIndex );
            if( pIndex < 0 )   intervalToStr += "        ";

            Console.WriteLine( "{0,21}", intervalToStr );
        }
        catch( Exception ex )
        {
            // If Parse throws an exception, write the message.
            Console.WriteLine( ex.Message );
        }
    } 

    static void Main( )
    {
        Console.WriteLine(
            "This example of TimeSpan.Parse( string ) and \n" +
            "TimeSpan.ToString( ) " +
            "generates the following output.\n" );
        Console.WriteLine( "{0,20}   {1,21}", 
            "String to Parse", "TimeSpan or Exception" );
        Console.WriteLine( "{0,20}   {1,21}", 
            "---------------", "---------------------" );

        ParseNDisplayTimeSpan( "0" );
        ParseNDisplayTimeSpan( "14" );
        ParseNDisplayTimeSpan( "1:2:3" );
        ParseNDisplayTimeSpan( "0:0:0.250" );
        ParseNDisplayTimeSpan( "10.20:30:40.50" );
        ParseNDisplayTimeSpan( "99.23:59:59.9999999" );
        ParseNDisplayTimeSpan( "0023:0059:0059.0099" );
        ParseNDisplayTimeSpan( "24:0:0" );
        ParseNDisplayTimeSpan( "0:60:0" );
        ParseNDisplayTimeSpan( "0:0:60" );
        ParseNDisplayTimeSpan( "10:" );
        ParseNDisplayTimeSpan( ":10" );
        ParseNDisplayTimeSpan( "10:20:" );
        ParseNDisplayTimeSpan( ".123" );
        ParseNDisplayTimeSpan( "10." );
        ParseNDisplayTimeSpan( "10.12" );
    } 
} 

/*
This example of TimeSpan.Parse( string ) and
TimeSpan.ToString( ) generates the following output.

     String to Parse   TimeSpan or Exception
     ---------------   ---------------------
                   0        00:00:00
                  14     14.00:00:00
               1:2:3        01:02:03
           0:0:0.250        00:00:00.2500000
      10.20:30:40.50     10.20:30:40.5000000
 99.23:59:59.9999999     99.23:59:59.9999999
 0023:0059:0059.0099        23:59:59.0099000
              24:0:0   TimeSpan overflowed because the duration is too long.
              0:60:0   TimeSpan overflowed because the duration is too long.
              0:0:60   TimeSpan overflowed because the duration is too long.
                 10:   Input string was not in a correct format.
                 :10   Input string was not in a correct format.
              10:20:   Input string was not in a correct format.
                .123   Input string was not in a correct format.
                 10.   Input string was not in a correct format.
               10.12   Input string was not in a correct format.
*/ 

// Example of the TimeSpan.Parse( string ) and TimeSpan.ToString( ) 
// methods.
import System.*;

class TSParseToStringDemo
{
    static void ParseNDisplayTimeSpan(String intervalStr)
    {
        // Write the first part of the output line.
        Console.Write("{0,20}   ", intervalStr);

        // Parse the parameter, and then convert it back to a string.
        try {
            TimeSpan intervalVal = TimeSpan.Parse(intervalStr);
            String intervalToStr = intervalVal.ToString();

            // Pad the end of the TimeSpan string with spaces if it 
            // does not contain milliseconds.
            int pIndex = intervalToStr.IndexOf(':');

            pIndex = intervalToStr.IndexOf('.', pIndex);
            if (pIndex < 0) {
                intervalToStr += "        ";
            }

            Console.WriteLine("{0,21}", intervalToStr);
        }
        catch (System.Exception ex) {
            // If Parse throws an exception, write the message.
            Console.WriteLine(ex.get_Message());
        }
    } //ParseNDisplayTimeSpan

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of TimeSpan.Parse( string ) and \n" 
            + "TimeSpan.ToString( ) " + "generates the following output.\n"));
        Console.WriteLine("{0,20}   {1,21}", "String to Parse",
            "TimeSpan or Exception");
        Console.WriteLine("{0,20}   {1,21}", "---------------",
            "---------------------");
        ParseNDisplayTimeSpan("0");
        ParseNDisplayTimeSpan("14");
        ParseNDisplayTimeSpan("1:2:3");
        ParseNDisplayTimeSpan("0:0:0.250");
        ParseNDisplayTimeSpan("10.20:30:40.50");
        ParseNDisplayTimeSpan("99.23:59:59.9999999");
        ParseNDisplayTimeSpan("0023:0059:0059.0099");
        ParseNDisplayTimeSpan("24:0:0");
        ParseNDisplayTimeSpan("0:60:0");
        ParseNDisplayTimeSpan("0:0:60");
        ParseNDisplayTimeSpan("10:");
        ParseNDisplayTimeSpan(":10");
        ParseNDisplayTimeSpan("10:20:");
        ParseNDisplayTimeSpan(".123");
        ParseNDisplayTimeSpan("10.");
        ParseNDisplayTimeSpan("10.12");
    } //main
} //TSParseToStringDemo

/*
This example of TimeSpan.Parse( string ) and
TimeSpan.ToString( ) generates the following output.

     String to Parse   TimeSpan or Exception
     ---------------   ---------------------
                   0        00:00:00
                  14     14.00:00:00
               1:2:3        01:02:03
           0:0:0.250        00:00:00.2500000
      10.20:30:40.50     10.20:30:40.5000000
 99.23:59:59.9999999     99.23:59:59.9999999
 0023:0059:0059.0099        23:59:59.0099000
              24:0:0   TimeSpan overflowed because the duration is too long.
              0:60:0   TimeSpan overflowed because the duration is too long.
              0:0:60   TimeSpan overflowed because the duration is too long.
                 10:   Input string was not in a correct format.
                 :10   Input string was not in a correct format.
              10:20:   Input string was not in a correct format.
                .123   Input string was not in a correct format.
                 10.   Input string was not in a correct format.
               10.12   Input string was not in a correct format.
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft