Mono Class Library: System.TimeSpan Overview | Members

System.TimeSpan.Parse Method

Returns the specified string converted to a TimeSpan value.

public static TimeSpan Parse (string s)

Parameters

s

A string containing the value to convert. s contains a time interval in the following form:

[ws][-][d.]hh:mm:ss[.ff][ws]

Items in square brackets ('[' and']') are optional. Colons and periods (':' and'.') are literal characters. For details on the remaining symbols, see the description section.

Returns

The TimeSpan value obtained from s.

Exceptions

TypeReason
ArgumentNullExceptions is a null reference.
FormatExceptions is in an invalid format.
OverflowException

s represents a number greater than TimeSpan.MaxValue or less than TimeSpan.MinValue.

-or-

At least one of the hours, minutes, or seconds components is outside its valid range.

Remarks

The symbols used in the parameter description for s are as follows:

ItemDescription
wsWhite space (zero or more space and/or tab characters).
"-"Minus sign, indicating a negative time interval.
"d"Days.
"hh"Hours, ranging from 0 to 23 inclusive.
"mm"Minutes, ranging from 0 to 59 inclusive.
"ss"Seconds, ranging from 0 to 59 inclusive.
"ff"Fractional seconds, from 1 to 7 decimal digits inclusive.

Example

This example demonstrates parsing a string to obtain a TimeSpan.

C# Example

using System;
public class TimeSpanParseExample {
   public static void Main() {
      String str = "    -5.12:34:56.789     ";
      TimeSpan ts = TimeSpan.Parse(str);
      Console.WriteLine(@"The string ""{0}""", str); 
      Console.WriteLine("parses to TimeSpan {0}", ts);
   }
}
   

The output is

Example

The string "    -5.12:34:56.789     "
parses to TimeSpan -5.12:34:56.7890000
 

Requirements

Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0