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

Updated: April 2009

Returns the string representation of the value of this instance.

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

public override string ToString()

Return Value

Type: System.String
A string that represents the value of this instance. The return value is of the form:
[-][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.

Item

Description

"-"

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

"d"

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

"hh"

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

"mm"

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

"ss"

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

"fffffff"

Fractional seconds in the time span. This element is omitted if the time span does not include fractional seconds. If present, fractional seconds are always expressed using 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.

Note that the ToString method does not include an overload that includes format specifiers and provides precise control over the returned string. To define the precise format of the returned string, you can format a TimeSpan object's individual property values, as the following example shows. For more information, see Numeric Format Strings.

using System;

public class ToString
{
   public static void Main()
   {
      TimeSpan span;

      // Initialize a time span to zero.
      span = TimeSpan.Zero;
      Console.WriteLine(FormatTimeSpan(span, true));

      // Initialize a time span to 14 days.
      span = new TimeSpan(-14, 0, 0, 0, 0);
      Console.WriteLine(FormatTimeSpan(span, true));

      // Initialize a time span to 1:02:03.
      span = new TimeSpan(1, 2, 3);
      Console.WriteLine(FormatTimeSpan(span, false));


      // Initialize a time span to 250 milliseconds.
      span = new TimeSpan(0, 0, 0, 0, 250);
      Console.WriteLine(FormatTimeSpan(span, true));

      // Initalize a time span to 99 days, 23 hours, 59 minutes, and 59.9999999 seconds.
      span = new TimeSpan(99, 23, 59, 59, 999);
      Console.WriteLine(FormatTimeSpan(span, false));

      // Initalize a timespan to 25 milliseconds.
      span = new TimeSpan(0, 0, 0, 0, 25);
      Console.WriteLine(FormatTimeSpan(span, false));
   }

   private static string FormatTimeSpan(TimeSpan span, bool showSign)
   {
      string sign = String.Empty;
      if (showSign && (span > TimeSpan.Zero)) 
         sign = "+";  

      return sign + span.Days.ToString("00") + "." + 
             span.Hours.ToString("00") + ":" + 
             span.Minutes.ToString("00") + ":" + 
             span.Seconds.ToString("00") + "." + 
             span.Milliseconds.ToString("000");
   }
}
// This example displays the following output: 
//       00.00:00:00.000 
//       -14.00:00:00.000 
//       00.01:02:03.000 
//       +00.00:00:00.250 
//       99.23:59:59.999 
//       00.00:00:00.025

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

using System;

public class ToString
{
   public static void Main()
   {
      TimeSpan span;

      // Initialize a time span to zero.
      span = TimeSpan.Zero;
      Console.WriteLine(span);

      // Initialize a time span to 14 days.
      span = new TimeSpan(-14, 0, 0, 0, 0);
      Console.WriteLine(span);

      // Initialize a time span to 1:02:03.
      span = new TimeSpan(1, 2, 3);
      Console.WriteLine(span);


      // Initialize a time span to 250 milliseconds.
      span = new TimeSpan(0, 0, 0, 0, 250);
      Console.WriteLine(span);

      // Initalize a time span to 99 days, 23 hours, 59 minutes, and 59.999 seconds.
      span = new TimeSpan(99, 23, 59, 59, 999);
      Console.WriteLine(span);

      // Initalize a time span to 3 hours.
      span = new TimeSpan(3, 0, 0);
      Console.WriteLine(span);

      // Initalize a timespan to 25 milliseconds.
      span = new TimeSpan(0, 0, 0, 0, 25);
      Console.WriteLine(span);
   }
}
// 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

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

April 2009

Replaced the duplicated Visual Basic example.

Customer feedback.

July 2008

Revised the explanation of the format of the returned string; added examples.

Customer feedback.

Community Additions

Show:
© 2014 Microsoft