Returns the specified string converted to a TimeSpan value.
- 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.
The TimeSpan value obtained from s.
Type Reason ArgumentNullException s is a null reference. FormatException s 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.
The symbols used in the parameter description for s are as follows:
Item Description ws White 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.
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
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0