C# TimeSpan ParseExact(String, String, IFormatProvider)

Description

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

Syntax

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


public static TimeSpan ParseExact(
  string input,/*from   www  .  jav  a  2s. co m*/
  string format,
  IFormatProvider formatProvider
)

Parameters

TimeSpan.ParseExact(String, String, IFormatProvider) 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.

Returns

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

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  .  ja va  2s  . co m*/
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string intervalString = "17:15", format = "g";
      TimeSpan interval;
      CultureInfo culture = CultureInfo.CurrentCulture;

      interval = TimeSpan.ParseExact(intervalString, format, culture);
      Console.WriteLine("'{0}' --> {1}", intervalString, interval);

      // Parse hour:minute:second value with "G" specifier.
      intervalString = "17:14:48";
      format = "G";
      culture = CultureInfo.InvariantCulture;

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