TimeSpan.TryParse Method (String, IFormatProvider, TimeSpan)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

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.

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)

public static bool TryParse(
	string input,
	IFormatProvider formatProvider,
	out TimeSpan result


Type: System.String

A string that specifies the time interval to convert.

Type: System.IFormatProvider

An object that supplies culture-specific formatting information.

Type: System.TimeSpan

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.

Return Value

Type: System.Boolean

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.

The TryParse(String, IFormatProvider, TimeSpan) method is like the Parse(String, IFormatProvider) method, except that it does not throw an exception if the conversion fails.

The input parameter contains a time interval specification in the form:

[ws][-]{ d | d.hh:mm[:ss[.ff]] | hh:mm[:ss[.ff]] }[ws]

Elements in square brackets ([ and ]) are optional. One selection from the list of alternatives enclosed in braces ({ and }) and separated by vertical bars (|) is required. The following table describes each element.




Optional white space.


An optional minus sign, which indicates a negative TimeSpan.


Days, ranging from 0 to 10675199.


A culture-sensitive symbol that separates days from hours. The invariant format uses a period (".") character.


Hours, ranging from 0 to 23.


The culture-sensitive time separator symbol. The invariant format uses a colon (":") character.


Minutes, ranging from 0 to 59.


Optional seconds, ranging from 0 to 59.


A culture-sensitive symbol that separates seconds from fractions of a second. The invariant format uses a period (".") character.


Optional fractional seconds, consisting of one to seven decimal digits.

The components of input must collectively specify a time interval that is greater than or equal to TimeSpan.MinValue and less than or equal to TimeSpan.MaxValue.

The TryParse(String, IFormatProvider, TimeSpan) method tries to parse input by using each of the culture-specific formats for the culture specified by formatProvider.

The formatProvider parameter is an IFormatProvider implementation that provides culture-specific information about the format of the returned string. The formatProvider parameter can be any of the following:

If formatProvider is null, the DateTimeFormatInfo object that is associated with the current culture is used.

Notes to Callers:

In some cases, when a time interval component in the string to be parsed contains more than seven digits, parsing operations that succeed and return true in the .NET Framework 3.5 and earlier versions may fail and return false in the .NET Framework 4, The following example illustrates this scenario.

string value = "000000006";
TimeSpan interval;
if (TimeSpan.TryParse(value, out interval))
   Console.WriteLine("{0} --> {1}", value, interval);
   Console.WriteLine("Unable to parse '{0}'", value);
// Output from .NET Framework 3.5 and earlier versions:
//       000000006 --> 6.00:00:00
// Output from .NET Framework 4:
//       Unable to parse //000000006//

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. The example illustrates how the conventions of a specific culture influence the formatting operation.

using System;
using System.Globalization;

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

      string header = String.Format("{0,-17}", "String");
      foreach (CultureInfo culture in cultures)
         header += culture.Equals(CultureInfo.InvariantCulture) ? 
                      String.Format("{0,20}", "Invariant") :
                      String.Format("{0,20}", culture.Name);


      foreach (string value in values)
         Console.Write("{0,-17}", value);
         foreach (CultureInfo culture in cultures)
            TimeSpan interval = new TimeSpan();
            if (TimeSpan.TryParse(value, culture, out interval))
               Console.Write("{0,20}", interval.ToString("c"));
               Console.Write("{0,20}", "Unable to Parse");
// The example displays the following output:
//    String                          en-US               ru-RU           Invariant
//    6                          6.00:00:00          6.00:00:00          6.00:00:00
//    6:12                         06:12:00            06:12:00            06:12:00
//    6:12:14                      06:12:14            06:12:14            06:12:14
//    6:12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6.12:14:45                 6.12:14:45          6.12:14:45          6.12:14:45
//    6:12:14:45.3448    6.12:14:45.3448000     Unable to Parse  6.12:14:45.3448000
//    6:12:14:45,3448       Unable to Parse  6.12:14:45.3448000     Unable to Parse
//    6:34:14:45            Unable to Parse     Unable to Parse     Unable to Parse

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 4.0
Windows Phone Silverlight
Available since 7.1
Windows Phone
Available since 8.1
Return to top