Mono Class Library: System.TimeSpan Overview | Members

System.TimeSpan.ToString Method

Returns a string representation of the value of the current instance.

public override string ToString ()

Returns

A string representation of the current instance formatted as follows:

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

Items in square brackets ('[' and ']') are included provisionally: '-' is included if and only if the current instance is negative; "d." and ".ff" are included if and only if those components are non-zero. Colons and periods (':' and '.') are literal characters. Other components are as follows.

ComponentDescription
"-"Minus sign, indicating a negative time interval.
"d"Days.
"hh"Hours, ranging from 0 to 23 inclusive.
"mm"Minutes, ranging from 0 to 59 inclusive.
"ss"Seconds, ranging from 0 to 59 inclusive.
"ff"Fractional seconds.

Remarks

Note: This method overrides object.ToString.

Example

This example demonstrates the TimeSpan.ToString method.

C# Example

using System;
public class TimeSpanToStringExample {
   public static void Main() {
      TimeSpan tsOne = new TimeSpan(1, 23, 45, 54, 321);
      TimeSpan tsTwo = new TimeSpan(0, 23, 45, 54, 0);
      Console.Write("TimeSpan one, with d. and .ff: ");
      Console.WriteLine("{0}", tsOne.ToString());
      Console.Write("TimeSpan two, without d. and .ff: ");
      Console.WriteLine("{0}", tsTwo.ToString());
   }
}

The output is

TimeSpan one, with d. and .ff: 1.23:45:54.3210000
TimeSpan two, without d. and .ff: 23:45:54

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0