Export (0) Print
Expand All

TimeSpan.ToString Method (String, IFormatProvider)

Converts the value of the current TimeSpan object to its equivalent string representation by using the specified format and culture-specific formatting information.

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

public string ToString(
	string format,
	IFormatProvider formatProvider
)

Parameters

format
Type: System.String

A standard or custom TimeSpan format string.

formatProvider
Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Return Value

Type: System.String
The string representation of the current TimeSpan value, as specified by format and formatProvider.

Implements

IFormattable.ToString(String, IFormatProvider)

ExceptionCondition
FormatException

The format parameter is not recognized or is not supported.

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.

Important noteImportant

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.

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

The formatProvider parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string. The formatProvider parameter can be any of the following:

If formatProvider is null, the DateTimeFormatInfo object that is associated with the current culture is used. If format is a custom format string, the formatProvider parameter is ignored.

The following example calls the ToString(String, IFormatProvider) method to format two time intervals. The example calls the method twice for each format string, first to display it using the conventions of the en-US culture and then to display it using the conventions of the fr-FR culture.

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      TimeSpan[] intervals = { new TimeSpan(38, 30, 15), 
                               new TimeSpan(16, 14, 30) }; 
      CultureInfo[] cultures = { new CultureInfo("en-US"), 
                                 new CultureInfo("fr-FR") };
      string[] formats = {"c", "g", "G", @"hh\:mm\:ss" };
      Console.WriteLine("{0,12}      Format  {1,22}  {2,22}\n", 
                        "Interval", cultures[0].Name, cultures[1].Name);

      foreach (var interval in intervals) {
         foreach (var fmt in formats)
            Console.WriteLine("{0,12}  {1,10}  {2,22}  {3,22}", 
                              interval, fmt, 
                              interval.ToString(fmt, cultures[0]), 
                              interval.ToString(fmt, cultures[1]));
         Console.WriteLine();
      }  
   }
}
// The example displays the following output: 
//        Interval      Format                   en-US                   fr-FR 
//     
//      1.14:30:15           c              1.14:30:15              1.14:30:15 
//      1.14:30:15           g              1:14:30:15              1:14:30:15 
//      1.14:30:15           G      1:14:30:15.0000000      1:14:30:15,0000000 
//      1.14:30:15  hh\:mm\:ss                14:30:15                14:30:15 
//     
//        16:14:30           c                16:14:30                16:14:30 
//        16:14:30           g                16:14:30                16:14:30 
//        16:14:30           G      0:16:14:30.0000000      0:16:14:30,0000000 
//        16:14:30  hh\:mm\:ss                16:14:30                16:14:30

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft