C# TimeSpan ToString(String, IFormatProvider)

Description

TimeSpan ToString(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.

Syntax

TimeSpan.ToString(String, IFormatProvider) has the following syntax.


public string ToString(
  string format,
  IFormatProvider formatProvider
)

Parameters

TimeSpan.ToString(String, IFormatProvider) has the following parameters.

  • format - A standard or custom TimeSpan format string.
  • formatProvider - An object that supplies culture-specific formatting information.

Returns

TimeSpan.ToString(String, IFormatProvider) method returns The string representation of the current TimeSpan value, as specified by format and formatProvider.

Example

The following example calls the ToString(String, IFormatProvider) method to format two time intervals.


//from w  w  w .j av a2 s .c  o m
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" };
      foreach (var interval in intervals) {
         foreach (var fmt in formats){
            Console.WriteLine(interval.ToString(fmt, cultures[0])); 
            Console.WriteLine(interval.ToString(fmt, cultures[1]));
         }
      }  
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version