Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
System Namespace
TimeSpan Structure
TimeSpan Methods
 Parse Method
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
TimeSpan..::.Parse Method

Updated: November 2007

Constructs a new TimeSpan object from a time interval specified in a string.

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

Visual Basic (Declaration)
Public Shared Function Parse ( _
    s As String _
) As TimeSpan
Visual Basic (Usage)
Dim s As String
Dim returnValue As TimeSpan

returnValue = TimeSpan.Parse(s)
C#
public static TimeSpan Parse(
    string s
)
Visual C++
public:
static TimeSpan Parse(
    String^ s
)
J#
public static TimeSpan Parse(
    String s
)
JScript
public static function Parse(
    s : String
) : TimeSpan

Parameters

s
Type: System..::.String

A string that specifies a time interval.

Return Value

Type: System..::.TimeSpan

A TimeSpan that corresponds to s.

ExceptionCondition
ArgumentNullException

s is nullNothingnullptra null reference (Nothing in Visual Basic).

FormatException

s has an invalid format.

OverflowException

s represents a number less than MinValue or greater than 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 of the form:

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

Items in square brackets ([ and ]) are optional; one selection from the list of alternatives enclosed in braces ({ and }) and separated by vertical bars (|) is required; colons and periods (: and .) are literal characters and required; other items are as follows.

Item

Description

ws

optional white space

"-"

optional minus sign indicating a negative TimeSpan

d

days, ranging from 0 to 10675199

hh

hours, ranging from 0 to 23

mm

minutes, ranging from 0 to 59

ss

optional seconds, ranging from 0 to 59

ff

optional fractional seconds, consisting of 1 to 7 decimal digits

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

The following code example uses the Parse method to create TimeSpan objects from valid TimeSpan strings and to raise exceptions from invalid TimeSpan strings.

Visual Basic
' Example of the TimeSpan.Parse( String ) and TimeSpan.ToString( ) 
' methods.
Imports System
Imports Microsoft.VisualBasic

Module TSParseToStringDemo

    Sub ParseNDisplayTimeSpan( intervalStr As String )

        ' Write the first part of the output line.
        Console.Write( "{0,20}   ", intervalStr )

        ' Parse the parameter, and then convert it back to a string.
        Try
            Dim intervalVal As TimeSpan = TimeSpan.Parse( intervalStr )
            Dim intervalToStr As String = intervalVal.ToString( )

            ' Pad the end of the TimeSpan string with spaces if it 
            ' does not contain milliseconds.
            Dim pIndex As Integer = intervalToStr.IndexOf( ":"c )
            pIndex = intervalToStr.IndexOf( "."c, pIndex )
            If pIndex < 0 Then   intervalToStr &= "        "

            Console.WriteLine( "{0,21}", intervalToStr )

        ' If Parse throws an exception, write the message.
        Catch ex As Exception
            Console.WriteLine( ex.Message )
        End Try
    End Sub 

    Sub Main( )

        Console.WriteLine( _
            "This example of TimeSpan.Parse( String ) and " & _
            vbCrLf & "TimeSpan.ToString( ) " & _
            "generates the following output." & vbCrLf )
        Console.WriteLine( "{0,20}   {1,21}", _
            "String to Parse", "TimeSpan or Exception" )    
        Console.WriteLine( "{0,20}   {1,21}", _
            "---------------", "---------------------" )    

        ParseNDisplayTimeSpan( "0" )
        ParseNDisplayTimeSpan( "14" )
        ParseNDisplayTimeSpan( "1:2:3" )
        ParseNDisplayTimeSpan( "0:0:0.250" )
        ParseNDisplayTimeSpan( "10.20:30:40.50" )
        ParseNDisplayTimeSpan( "99.23:59:59.9999999" )
        ParseNDisplayTimeSpan( "0023:0059:0059.0099" )
        ParseNDisplayTimeSpan( "24:0:0" )
        ParseNDisplayTimeSpan( "0:60:0" )
        ParseNDisplayTimeSpan( "0:0:60" )
        ParseNDisplayTimeSpan( "10:" )
        ParseNDisplayTimeSpan( ":10" )
        ParseNDisplayTimeSpan( "10:20:" )
        ParseNDisplayTimeSpan( ".123" )
        ParseNDisplayTimeSpan( "10." )
        ParseNDisplayTimeSpan( "10.12" )
    End Sub 
End Module 

' This example of TimeSpan.Parse( String ) and
' TimeSpan.ToString( ) generates the following output.
' 
'      String to Parse   TimeSpan or Exception
'      ---------------   ---------------------
'                    0        00:00:00
'                   14     14.00:00:00
'                1:2:3        01:02:03
'            0:0:0.250        00:00:00.2500000
'       10.20:30:40.50     10.20:30:40.5000000
'  99.23:59:59.9999999     99.23:59:59.9999999
'  0023:0059:0059.0099        23:59:59.0099000
'               24:0:0   TimeSpan overflowed because the duration is too long.
'               0:60:0   TimeSpan overflowed because the duration is too long.
'               0:0:60   TimeSpan overflowed because the duration is too long.
'                  10:   Input string was not in a correct format.
'                  :10   Input string was not in a correct format.
'               10:20:   Input string was not in a correct format.
'                 .123   Input string was not in a correct format.
'                  10.   Input string was not in a correct format.
'                10.12   Input string was not in a correct format.

C#
// Example of the TimeSpan.Parse( string ) and TimeSpan.ToString( ) 
// methods.
using System;

class TSParseToStringDemo
{
    static void ParseNDisplayTimeSpan( string intervalStr )
    {
        // Write the first part of the output line.
        Console.Write( "{0,20}   ", intervalStr );

        // Parse the parameter, and then convert it back to a string.
        try
        {
            TimeSpan intervalVal = TimeSpan.Parse( intervalStr );
            string   intervalToStr = intervalVal.ToString( );

            // Pad the end of the TimeSpan string with spaces if it 
            // does not contain milliseconds.
            int pIndex = intervalToStr.IndexOf( ':' );
            pIndex = intervalToStr.IndexOf( '.', pIndex );
            if( pIndex < 0 )   intervalToStr += "        ";

            Console.WriteLine( "{0,21}", intervalToStr );
        }
        catch( Exception ex )
        {
            // If Parse throws an exception, write the message.
            Console.WriteLine( ex.Message );
        }
    } 

    static void Main( )
    {
        Console.WriteLine(
            "This example of TimeSpan.Parse( string ) and \n" +
            "TimeSpan.ToString( ) " +
            "generates the following output.\n" );
        Console.WriteLine( "{0,20}   {1,21}", 
            "String to Parse", "TimeSpan or Exception" );
        Console.WriteLine( "{0,20}   {1,21}", 
            "---------------", "---------------------" );

        ParseNDisplayTimeSpan( "0" );
        ParseNDisplayTimeSpan( "14" );
        ParseNDisplayTimeSpan( "1:2:3" );
        ParseNDisplayTimeSpan( "0:0:0.250" );
        ParseNDisplayTimeSpan( "10.20:30:40.50" );
        ParseNDisplayTimeSpan( "99.23:59:59.9999999" );
        ParseNDisplayTimeSpan( "0023:0059:0059.0099" );
        ParseNDisplayTimeSpan( "24:0:0" );
        ParseNDisplayTimeSpan( "0:60:0" );
        ParseNDisplayTimeSpan( "0:0:60" );
        ParseNDisplayTimeSpan( "10:" );
        ParseNDisplayTimeSpan( ":10" );
        ParseNDisplayTimeSpan( "10:20:" );
        ParseNDisplayTimeSpan( ".123" );
        ParseNDisplayTimeSpan( "10." );
        ParseNDisplayTimeSpan( "10.12" );
    } 
} 

/*
This example of TimeSpan.Parse( string ) and
TimeSpan.ToString( ) generates the following output.

     String to Parse   TimeSpan or Exception
     ---------------   ---------------------
                   0        00:00:00
                  14     14.00:00:00
               1:2:3        01:02:03
           0:0:0.250        00:00:00.2500000
      10.20:30:40.50     10.20:30:40.5000000
 99.23:59:59.9999999     99.23:59:59.9999999
 0023:0059:0059.0099        23:59:59.0099000
              24:0:0   TimeSpan overflowed because the duration is too long.
              0:60:0   TimeSpan overflowed because the duration is too long.
              0:0:60   TimeSpan overflowed because the duration is too long.
                 10:   Input string was not in a correct format.
                 :10   Input string was not in a correct format.
              10:20:   Input string was not in a correct format.
                .123   Input string was not in a correct format.
                 10.   Input string was not in a correct format.
               10.12   Input string was not in a correct format.
*/ 

Visual C++
// Example of the TimeSpan::Parse( String* ) and TimeSpan::ToString( ) 
// methods.
using namespace System;
void ParseNDisplayTimeSpan( String^ intervalStr )
{

   // Write the first part of the output line.
   Console::Write( "{0,20}   ", intervalStr );

   // Parse the parameter, and then convert it back to a string.
   try
   {
      TimeSpan intervalVal = TimeSpan::Parse( intervalStr );
      String^ intervalToStr = intervalVal.ToString();

      // Pad the end of the TimeSpan string with spaces if it 
      // does not contain milliseconds.
      int pIndex = intervalToStr->IndexOf( ':' );
      pIndex = intervalToStr->IndexOf( '.', pIndex );
      if ( pIndex < 0 )
            intervalToStr = String::Concat( intervalToStr, "        " );
      Console::WriteLine( "{0,21}", intervalToStr );
   }
   catch ( Exception^ ex ) 
   {

      // If Parse throws an exception, write the message.
      Console::WriteLine( ex->Message );
   }

}

int main()
{
   Console::WriteLine( "This example of TimeSpan::Parse( String* ) and \n"
   "TimeSpan::ToString( ) "
   "generates the following output.\n" );
   Console::WriteLine( "{0,20}   {1,21}", "String to Parse", "TimeSpan or Exception" );
   Console::WriteLine( "{0,20}   {1,21}", "---------------", "---------------------" );
   ParseNDisplayTimeSpan( "0" );
   ParseNDisplayTimeSpan( "14" );
   ParseNDisplayTimeSpan( "1:2:3" );
   ParseNDisplayTimeSpan( "0:0:0.250" );
   ParseNDisplayTimeSpan( "10.20:30:40.50" );
   ParseNDisplayTimeSpan( "99.23:59:59.9999999" );
   ParseNDisplayTimeSpan( "0023:0059:0059.0099" );
   ParseNDisplayTimeSpan( "24:0:0" );
   ParseNDisplayTimeSpan( "0:60:0" );
   ParseNDisplayTimeSpan( "0:0:60" );
   ParseNDisplayTimeSpan( "10:" );
   ParseNDisplayTimeSpan( ":10" );
   ParseNDisplayTimeSpan( "10:20:" );
   ParseNDisplayTimeSpan( ".123" );
   ParseNDisplayTimeSpan( "10." );
   ParseNDisplayTimeSpan( "10.12" );
}

/*
This example of TimeSpan::Parse( String* ) and
TimeSpan::ToString( ) generates the following output.

     String to Parse   TimeSpan or Exception
     ---------------   ---------------------
                   0        00:00:00
                  14     14.00:00:00
               1:2:3        01:02:03
           0:0:0.250        00:00:00.2500000
      10.20:30:40.50     10.20:30:40.5000000
 99.23:59:59.9999999     99.23:59:59.9999999
 0023:0059:0059.0099        23:59:59.0099000
              24:0:0   TimeSpan overflowed because the duration is too long.
              0:60:0   TimeSpan overflowed because the duration is too long.
              0:0:60   TimeSpan overflowed because the duration is too long.
                 10:   Input string was not in a correct format.
                 :10   Input string was not in a correct format.
              10:20:   Input string was not in a correct format.
                .123   Input string was not in a correct format.
                 10.   Input string was not in a correct format.
               10.12   Input string was not in a correct format.
*/

J#
// Example of the TimeSpan.Parse( string ) and TimeSpan.ToString( ) 
// methods.
import System.*;

class TSParseToStringDemo
{
    static void ParseNDisplayTimeSpan(String intervalStr)
    {
        // Write the first part of the output line.
        Console.Write("{0,20}   ", intervalStr);

        // Parse the parameter, and then convert it back to a string.
        try {
            TimeSpan intervalVal = TimeSpan.Parse(intervalStr);
            String intervalToStr = intervalVal.ToString();

            // Pad the end of the TimeSpan string with spaces if it 
            // does not contain milliseconds.
            int pIndex = intervalToStr.IndexOf(':');

            pIndex = intervalToStr.IndexOf('.', pIndex);
            if (pIndex < 0) {
                intervalToStr += "        ";
            }

            Console.WriteLine("{0,21}", intervalToStr);
        }
        catch (System.Exception ex) {
            // If Parse throws an exception, write the message.
            Console.WriteLine(ex.get_Message());
        }
    } //ParseNDisplayTimeSpan

    public static void main(String[] args)
    {
        Console.WriteLine(("This example of TimeSpan.Parse( string ) and \n" 
            + "TimeSpan.ToString( ) " + "generates the following output.\n"));
        Console.WriteLine("{0,20}   {1,21}", "String to Parse",
            "TimeSpan or Exception");
        Console.WriteLine("{0,20}   {1,21}", "---------------",
            "---------------------");
        ParseNDisplayTimeSpan("0");
        ParseNDisplayTimeSpan("14");
        ParseNDisplayTimeSpan("1:2:3");
        ParseNDisplayTimeSpan("0:0:0.250");
        ParseNDisplayTimeSpan("10.20:30:40.50");
        ParseNDisplayTimeSpan("99.23:59:59.9999999");
        ParseNDisplayTimeSpan("0023:0059:0059.0099");
        ParseNDisplayTimeSpan("24:0:0");
        ParseNDisplayTimeSpan("0:60:0");
        ParseNDisplayTimeSpan("0:0:60");
        ParseNDisplayTimeSpan("10:");
        ParseNDisplayTimeSpan(":10");
        ParseNDisplayTimeSpan("10:20:");
        ParseNDisplayTimeSpan(".123");
        ParseNDisplayTimeSpan("10.");
        ParseNDisplayTimeSpan("10.12");
    } //main
} //TSParseToStringDemo

/*
This example of TimeSpan.Parse( string ) and
TimeSpan.ToString( ) generates the following output.

     String to Parse   TimeSpan or Exception
     ---------------   ---------------------
                   0        00:00:00
                  14     14.00:00:00
               1:2:3        01:02:03
           0:0:0.250        00:00:00.2500000
      10.20:30:40.50     10.20:30:40.5000000
 99.23:59:59.9999999     99.23:59:59.9999999
 0023:0059:0059.0099        23:59:59.0099000
              24:0:0   TimeSpan overflowed because the duration is too long.
              0:60:0   TimeSpan overflowed because the duration is too long.
              0:0:60   TimeSpan overflowed because the duration is too long.
                 10:   Input string was not in a correct format.
                 :10   Input string was not in a correct format.
              10:20:   Input string was not in a correct format.
                .123   Input string was not in a correct format.
                 10.   Input string was not in a correct format.
               10.12   Input string was not in a correct format.
*/

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker