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

Description

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

Syntax

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


public static TimeSpan ParseExact(
  string input,/*from   w w  w .  j a  v  a2s.c o  m*/
  string[] formats,
  IFormatProvider formatProvider,
  TimeSpanStyles styles
)

Parameters

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

  • input - A string that specifies the time interval to convert.
  • formats - A array of standard or custom format strings that define the required format of input.
  • formatProvider - An object that provides culture-specific formatting information.
  • styles - A bitwise combination of enumeration values that defines the style elements that may be present in input.

Returns

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

Example

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


//from  w ww  .  ja va2s  . com

using System;
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 = { "%h", "g", "G" };
      TimeSpan interval;
      CultureInfo culture = new CultureInfo("de-DE");

      // Parse each string in inputs using formats and the de-DE culture. 
      foreach (string input in inputs) {
         interval = TimeSpan.ParseExact(input, formats, culture,
                                           TimeSpanStyles.AssumeNegative);
         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