C# TimeSpan ParseExact(String, String, IFormatProvider, TimeSpanStyles)

Description

TimeSpan ParseExact(String, String, IFormatProvider, TimeSpanStyles) converts the string representation of a time interval to its TimeSpan equivalent by using the specified format, culture-specific format information, and styles. The format of the string representation must match the specified format 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 a2 s  . c o m
  string format,
  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.
  • format - A standard or custom format string that defines 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 format, formatProvider, and styles.

Example

The following example uses the ParseExact(String, String, IFormatProvider) method to parse several string representations of time intervals using various format strings and cultures.


using System;//from w w  w. j  a va  2  s  . co  m
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString, format;
      TimeSpan interval;
      CultureInfo culture = null;

      // Parse hour:minute value with custom format specifier.
      intervalString = "17:15";
      format = "h\\:mm";
      culture = CultureInfo.CurrentCulture;

      interval = TimeSpan.ParseExact(intervalString, format, 
                                        culture, TimeSpanStyles.AssumeNegative);
      Console.WriteLine("'{0}' ({1}) --> {2}", intervalString, format, 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