C# TimeSpan ParseExact(String, String[], IFormatProvider) Array

Description

TimeSpan ParseExact(String, String[], IFormatProvider) converts the string representation of a time interval to its TimeSpan equivalent by using the specified array of format strings and culture-specific format information. The format of the string representation must match one of the specified formats exactly.

Syntax

TimeSpan.ParseExact(String, String[], IFormatProvider) has the following syntax.


public static TimeSpan ParseExact(
  string input,//w  w w  . j  av a 2 s  .  com
  string[] formats,
  IFormatProvider formatProvider
)

Parameters

TimeSpan.ParseExact(String, String[], IFormatProvider) has the following parameters.

  • input - A string that specifies the time interval to convert.
  • formats - A array of standard or custom format strings that defines the required format of input.
  • formatProvider - An object that provides culture-specific formatting information.

Returns

TimeSpan.ParseExact(String, String[], IFormatProvider) method returns A time interval that corresponds to input, as specified by formats and formatProvider.

Example

The following example calls the ParseExact(String, String[], IFormatProvider) method to convert each element of a string array to a TimeSpan value.


using System;//from w  ww  . j a  v a2  s .  c  o m
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] inputs = { "3", "16:42", "1:6:52:35.0625", 
                          "1:6:52:35,0625" }; 
      string[] formats = { "g", "G", "%h"};
      TimeSpan interval;
      CultureInfo culture = new CultureInfo("fr-FR");

      // Parse each string in inputs using formats and the fr-FR culture. 
      foreach (string input in inputs) {
         interval = TimeSpan.ParseExact(input, formats, culture);
         Console.WriteLine("{0} --> {1:c}", input, interval);
      }
   }
}

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