TimeSpan.ToString Method (System) (original) (raw)

Source:

TimeSpan.cs

Source:

TimeSpan.cs

Source:

TimeSpan.cs

Source:

TimeSpan.cs

Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format.

public:
 System::String ^ ToString(System::String ^ format);
public string ToString(string format);
public string ToString(string? format);
override this.ToString : string -> string
Public Function ToString (format As String) As String

Parameters

Returns

The string representation of the current TimeSpan value in the format specified by the format parameter.

Exceptions

The format parameter is not recognized or is not supported.

Examples

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

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", @"hh\:mm\:ss", "%m' min.'" };
foreach (TimeSpan span in spans)
{
   foreach (string fmt in fmts)
      Console.WriteLine("{0}: {1}", fmt, span.ToString(fmt));

   Console.WriteLine();
}
// The example displays the following output:
//       c: 00:00:00
//       g: 0:00:00
//       G: 0:00:00:00.0000000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: -14.00:00:00
//       g: -14:0:00:00
//       G: -14:00:00:00.0000000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: 01:02:03
//       g: 1:02:03
//       G: 0:01:02:03.0000000
//       hh\:mm\:ss: 01:02:03
//       %m' min.': 2 min.
//       
//       c: 00:00:00.2500000
//       g: 0:00:00.25
//       G: 0:00:00:00.2500000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: 99.23:59:59.9990000
//       g: 99:23:59:59.999
//       G: 99:23:59:59.9990000
//       hh\:mm\:ss: 23:59:59
//       %m' min.': 59 min.
//       
//       c: 03:00:00
//       g: 3:00:00
//       G: 0:03:00:00.0000000
//       hh\:mm\:ss: 03:00:00
//       %m' min.': 0 min.
//       
//       c: 00:00:00.0250000
//       g: 0:00:00.025
//       G: 0:00:00:00.0250000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
let spans = 
    [| TimeSpan.Zero
       TimeSpan(-14, 0, 0, 0, 0)
       TimeSpan(1, 2, 3)
       TimeSpan(0, 0, 0, 0, 250)
       TimeSpan(99, 23, 59, 59, 999)
       TimeSpan(3, 0, 0)
       TimeSpan(0, 0, 0, 0, 25) |]


let fmts = [| "c"; "g"; "G"; @"hh\:mm\:ss"; "%m' min.'" |]
for span in spans do
    for fmt in fmts do
        printfn $"{fmt}: {span.ToString fmt}"
    printfn ""
// The example displays the following output:
//       c: 00:00:00
//       g: 0:00:00
//       G: 0:00:00:00.0000000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: -14.00:00:00
//       g: -14:0:00:00
//       G: -14:00:00:00.0000000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: 01:02:03
//       g: 1:02:03
//       G: 0:01:02:03.0000000
//       hh\:mm\:ss: 01:02:03
//       %m' min.': 2 min.
//       
//       c: 00:00:00.2500000
//       g: 0:00:00.25
//       G: 0:00:00:00.2500000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
//       
//       c: 99.23:59:59.9990000
//       g: 99:23:59:59.999
//       G: 99:23:59:59.9990000
//       hh\:mm\:ss: 23:59:59
//       %m' min.': 59 min.
//       
//       c: 03:00:00
//       g: 3:00:00
//       G: 0:03:00:00.0000000
//       hh\:mm\:ss: 03:00:00
//       %m' min.': 0 min.
//       
//       c: 00:00:00.0250000
//       g: 0:00:00.025
//       G: 0:00:00:00.0250000
//       hh\:mm\:ss: 00:00:00
//       %m' min.': 0 min.
Module Example
   Public Sub Main()
      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", "hh\:mm\:ss", "%m' min.'" }
      For Each span As TimeSpan In spans
         For Each fmt As String In fmts
            Console.WriteLine("{0}: {1}", fmt, span.ToString(fmt))
         Next
         Console.WriteLine()         
      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
'       hh\:mm\:ss: 00:00:00
'       %m' min.': 0 min.
'       
'       c: -14.00:00:00
'       g: -14:0:00:00
'       G: -14:00:00:00.0000000
'       hh\:mm\:ss: 00:00:00
'       %m' min.': 0 min.
'       
'       c: 01:02:03
'       g: 1:02:03
'       G: 0:01:02:03.0000000
'       hh\:mm\:ss: 01:02:03
'       %m' min.': 2 min.
'       
'       c: 00:00:00.2500000
'       g: 0:00:00.25
'       G: 0:00:00:00.2500000
'       hh\:mm\:ss: 00:00:00
'       %m' min.': 0 min.
'       
'       c: 99.23:59:59.9990000
'       g: 99:23:59:59.999
'       G: 99:23:59:59.9990000
'       hh\:mm\:ss: 23:59:59
'       %m' min.': 59 min.
'       
'       c: 03:00:00
'       g: 3:00:00
'       G: 0:03:00:00.0000000
'       hh\:mm\:ss: 03:00:00
'       %m' min.': 0 min.
'       
'       c: 00:00:00.0250000
'       g: 0:00:00.025
'       G: 0:00:00:00.0250000
'       hh\:mm\:ss: 00:00:00
'       %m' min.': 0 min.

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 null, 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.

If format is a standard format string, the format of the returned string is defined by the formatting conventions of the current culture.

Important

The custom format strings for TimeSpan values do not include a date or time separator. If you want to include these elements in your format string, you must treat them as character literals. See the example for an illustration, and see the Custom TimeSpan Format Strings topic for more information.

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

See also

Applies to