TimeSpan.Parse Method (String)

Converts the string representation of a time interval to its TimeSpan equivalent.

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

'Declaration
Public Shared Function Parse ( _
	s As String _
) As TimeSpan

Parameters

s
Type: System.String

A string that specifies the time interval to convert.

Return Value

Type: System.TimeSpan
A time interval that corresponds to s.

ExceptionCondition
ArgumentNullException

s is Nothing.

FormatException

s has an invalid format.

OverflowException

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

-or-

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

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

[ws][-]{ d | [d.]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.

Element

Description

ws

Optional white space.

-

An optional minus sign, which indicates a negative TimeSpan.

d

Days, ranging from 0 to 10675199.

.

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

hh

Hours, ranging from 0 to 23.

:

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

mm

Minutes, ranging from 0 to 59.

ss

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.

ff

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

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

The Parse(String) method tries to parse s by using each of the culture-specific formats for the current culture.

Notes to Callers

When a time interval component in the string to be parsed contains more than seven digits, parsing operations in the .NET Framework 3.5 and earlier versions may behave differently from parsing operations in the .NET Framework 4. In some cases, parsing operations that succeed in the .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in the .NET Framework 4. In other cases, parsing operations that throw a FormatException in the .NET Framework 3.5 and earlier versions may fail and throw an OverflowException in the .NET Framework 4. The following example illustrates both scenarios.

Dim values() As String = { "000000006", "12.12:12:12.12345678" }
For Each value As String In values
   Try 
      Dim interval As TimeSpan = TimeSpan.Parse(value)
      Console.WriteLine("{0} --> {1}", value, interval)
   Catch e As FormatException
      Console.WriteLine("{0}: Bad Format", value)
   Catch e As OverflowException
      Console.WriteLine("{0}: Overflow", value)
   End Try          
Next 
' Output from .NET Framework 3.5 and earlier versions: 
'       000000006 --> 6.00:00:00 
'       12.12:12:12.12345678: Bad Format       
' Output from .NET Framework 4: 
'       000000006: Overflow 
'       12.12:12:12.12345678: Overflow

The following example uses the Parse method to convert each element in a string array to a TimeSpan value. It changes the current system culture to Croatian - Croatia ("hr-HR") and English - United States ("en-US") to illustrate how the current system culture affects the parsing operation.

Imports System.Globalization
Imports System.Threading

Module Example
   Public Sub Main()
      Dim values() As String = { "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" }
      Dim cultureNames() As String = { "hr-HR", "en-US"}

      ' Change the current culture. 
      For Each cultureName As String In cultureNames
         Thread.CurrentThread.CurrentCulture = New CultureInfo(cultureName)
         Console.WriteLine("Current Culture: {0}", 
                           Thread.CurrentThread.CurrentCulture.Name)
         For Each value As String In values
            Try 
               Dim ts As TimeSpan = TimeSpan.Parse(value)
               Console.WriteLine("{0} --> {1}", value, ts.ToString("c"))
            Catch e As FormatException
               Console.WriteLine("{0}: Bad Format", value)
            Catch e As OverflowException
               Console.WriteLine("{0}: Overflow", value)
            End Try       
         Next 
         Console.WriteLine()                                
      Next 
   End Sub 
End Module 
' The example displays the following output: 
'       Current Culture: hr-HR 
'       6 --> 6.00:00:00 
'       6:12 --> 06:12:00 
'       6: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.3448: Bad Format 
'       6:12:14:45,3448 --> 6.12:14:45.3448000 
'       6:34:14:45: Overflow 
'        
'       Current Culture: en-US 
'       6 --> 6.00:00:00 
'       6:12 --> 06:12:00 
'       6: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.3448 --> 6.12:14:45.3448000 
'       6:12:14:45,3448: Bad Format 
'       6:34:14:45: Overflow

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft