C# TimeSpan TryParse(String, IFormatProvider, TimeSpan)

Description

TimeSpan TryParse(String, IFormatProvider, TimeSpan) converts the string representation of a time interval to its TimeSpan equivalent by using the specified culture-specific formatting information, and returns a value that indicates whether the conversion succeeded.

Syntax

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


public static bool TryParse(
  string input,//www .j a v a2 s  . c o m
  IFormatProvider formatProvider,
  out TimeSpan result
)

Parameters

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

  • input - A string that specifies the time interval to convert.
  • formatProvider - An object that supplies culture-specific formatting information.
  • result - When this method returns, contains an object that represents the time interval specified by input, or TimeSpan.Zero if the conversion failed. This parameter is passed uninitialized.

Returns

TimeSpan.TryParse(String, IFormatProvider, TimeSpan) method returns true if input was converted successfully; otherwise, false. This operation returns false if the input parameter is null or String.Empty, has an invalid format, represents a time interval that is less than TimeSpan.MinValue or greater than TimeSpan.MaxValue, or has at least one days, hours, minutes, or seconds component outside its valid range.

Example

The following example defines an array of CultureInfo objects, and uses each object in calls to the TryParse(String, IFormatProvider, TimeSpan) method to parse the elements in a string array.


/*from   w w  w.  j a v a 2 s . c om*/

using System;
using System.Globalization;

public class Example
{
    public static void Main()
    {
        string[] values = { "9", "9:12", "9:12:14", "9:12:14:45", 
                          "9.12:14:45", "9:12:14:45.1234", 
                          "9:12:14:45,1234", "9:34:14:45" };
        CultureInfo[] cultures = { new CultureInfo("en-US"), 
                                 new CultureInfo("ru-RU"),
                                 CultureInfo.InvariantCulture };

        foreach (CultureInfo culture in cultures)
            Console.WriteLine( culture.Equals(CultureInfo.InvariantCulture) ?
                         String.Format("{0,20}", "Invariant") :
                         String.Format("{0,20}", culture.Name));

        foreach (string value in values)
        {
            foreach (CultureInfo culture in cultures)
            {
                TimeSpan interval = new TimeSpan();
                if (TimeSpan.TryParse(value, culture, out interval))
                    Console.WriteLine("{0,20}", interval.ToString("c"));
                else
                    Console.WriteLine("{0,20}", "Unable to Parse");
            }
        }
    }
}

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