TimeSpan.ToString Method (String, IFormatProvider)
Microsoft Silverlight will reach end of support after October 2021. Learn more.
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)
Syntax
'Declaration
Public Function ToString ( _
format As String, _
formatProvider As IFormatProvider _
) As String
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
Exceptions
Exception | Condition |
---|---|
FormatException | The format parameter is not recognized or is not supported. |
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 nulla null reference (Nothing in Visual Basic), 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.
The .NET Framework provides extensive formatting support, which is described in greater detail in the following formatting topics:
For more information about format strings for TimeSpan values, see Standard TimeSpan Format Strings and Custom TimeSpan Format Strings.
For more information about support for formatting in the .NET Framework, see Formatting Types.
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:
A CultureInfo object that represents the culture whose formatting conventions are to be reflected in the returned string. The DateTimeFormatInfo object retuned by the CultureInfo.DateTimeFormat property defines the formatting of the returned string.
A DateTimeFormatInfo object that defines the formatting of the returned string.
A custom object that implements the IFormatProvider interface. Its IFormatProvider.GetFormat method returns a DateTimeFormatInfo object that provides formatting information.
If formatProvider is nulla null reference (Nothing in Visual Basic), the DateTimeFormatInfo object that is associated with the current culture is used.
Examples
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.
Imports System.Globalization
Module Example
Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
outputBlock.FontFamily = New System.Windows.Media.FontFamily("Courier New")
Dim intervals() As TimeSpan = { New TimeSpan(38, 30, 15),
New TimeSpan(16, 14, 30) }
Dim cultures() As CultureInfo = { New CultureInfo("en-US"),
New CultureInfo("fr-FR") }
Dim formats() As String = {"c", "g", "G"}
outputBlock.Text += String.Format("{0,16} Format {1,22} {2,22}",
"Interval", cultures(0).Name, cultures(1).Name) + vbCrLf
outputBlock.Text += vbCrLf
For Each interval In intervals
For Each fmt In formats
outputBlock.Text += String.Format("{0,16} {1,6} {2,22} {3,22}",
interval, fmt,
interval.ToString(fmt, cultures(0)),
interval.ToString(fmt, cultures(1))) + vbCrLf
Next
outputBlock.Text += vbCrLf
Next
End Sub
End Module
' 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
'
' 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
using System;
using System.Globalization;
public class Example
{
public static void Demo(System.Windows.Controls.TextBlock outputBlock)
{
outputBlock.FontFamily = new System.Windows.Media.FontFamily("Courier New");
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" };
outputBlock.Text += String.Format("{0,16} Format {1,22} {2,22}\n",
"Interval", cultures[0].Name, cultures[1].Name) + "\n";
foreach (var interval in intervals)
{
foreach (var fmt in formats)
outputBlock.Text += String.Format("{0,16} {1,6} {2,22} {3,22}",
interval, fmt,
interval.ToString(fmt, cultures[0]),
interval.ToString(fmt, cultures[1])) + "\n";
outputBlock.Text += "\n";
}
}
}
// 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
//
// 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
Version Information
Silverlight
Supported in: 5, 4
Silverlight for Windows Phone
Supported in: Windows Phone OS 7.1
Platforms
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.