This documentation is archived and is not being maintained.

TimeSpan Constructor

.NET Framework 1.1

Initializes a new TimeSpan.

Overload List

Initializes a new TimeSpan to the specified number of ticks.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Long)
[C#] public TimeSpan(long);
[C++] public: TimeSpan(__int64);
[JScript] public function TimeSpan(long);

Initializes a new TimeSpan to a specified number of hours, minutes, and seconds.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Integer, Integer, Integer)
[C#] public TimeSpan(int, int, int);
[C++] public: TimeSpan(int, int, int);
[JScript] public function TimeSpan(int, int, int);

Initializes a new TimeSpan to a specified number of days, hours, minutes, and seconds.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Integer, Integer, Integer, Integer)
[C#] public TimeSpan(int, int, int, int);
[C++] public: TimeSpan(int, int, int, int);
[JScript] public function TimeSpan(int, int, int, int);

Initializes a new TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds.

Supported by the .NET Compact Framework.

[Visual Basic] Public Sub New(Integer, Integer, Integer, Integer, Integer)
[C#] public TimeSpan(int, int, int, int, int);
[C++] public: TimeSpan(int, int, int, int, int);
[JScript] public function TimeSpan(int, int, int, int, int);

Example

[Visual Basic, C#, C++] The following code example creates several TimeSpan objects using the constructor overload that initializes a TimeSpan to a specified number of days, hours, minutes, seconds, and milliseconds.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of the TimeSpan constructor. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
' Example of the 
' TimeSpan( Integer, Integer, Integer, Integer, Integer ) constructor. 
Imports System
Imports Microsoft.VisualBasic

Module TimeSpanCtorIIIIIDemo

    ' Create a TimeSpan object and display its value.
    Sub CreateTimeSpan( days As Integer, hours As Integer, _
        minutes As Integer, seconds As Integer, millisec As Integer )

        Dim elapsedTime As New TimeSpan( _
            days, hours, minutes, seconds, millisec )

        ' Format the constructor for display.
        Dim ctor As String = _
            String.Format( "TimeSpan( {0}, {1}, {2}, {3}, {4} )", _
                days, hours, minutes, seconds, millisec )

        ' Display the constructor and its value.
        Console.WriteLine( "{0,-48}{1,24}", _
            ctor, elapsedTime.ToString( ) )
    End Sub

    Sub Main( )

        Console.WriteLine( _
            "This example of the " & vbCrLf & _
            "TimeSpan( Integer, Integer, " & _
            "Integer, Integer, Integer ) " & vbCrLf & _
            "constructor generates the following output." & vbCrLf )
        Console.WriteLine( "{0,-48}{1,16}", "Constructor", "Value" )
        Console.WriteLine( "{0,-48}{1,16}", "-----------", "-----" )

        CreateTimeSpan( 10, 20, 30, 40, 50 )
        CreateTimeSpan( -10, 20, 30, 40, 50 )
        CreateTimeSpan( 0, 0, 0, 0, 937840050 )
        CreateTimeSpan( 1111, 2222, 3333, 4444, 5555 )
        CreateTimeSpan( 1111, -2222, -3333, -4444, -5555 )
        CreateTimeSpan( 99999, 99999, 99999, 99999, 99999 )
    End Sub 
End Module 

' This example of the
' TimeSpan( Integer, Integer, Integer, Integer, Integer )
' constructor generates the following output.
' 
' Constructor                                                Value
' -----------                                                -----
' TimeSpan( 10, 20, 30, 40, 50 )                       10.20:30:40.0500000
' TimeSpan( -10, 20, 30, 40, 50 )                      -9.03:29:19.9500000
' TimeSpan( 0, 0, 0, 0, 937840050 )                    10.20:30:40.0500000
' TimeSpan( 1111, 2222, 3333, 4444, 5555 )           1205.22:47:09.5550000
' TimeSpan( 1111, -2222, -3333, -4444, -5555 )       1016.01:12:50.4450000
' TimeSpan( 99999, 99999, 99999, 99999, 99999 )    104236.05:27:18.9990000

[C#] 
// Example of the TimeSpan( int, int, int, int, int ) constructor. 
using System;

class TimeSpanCtorIIIIIDemo
{
    // Create a TimeSpan object and display its value.
    static void CreateTimeSpan( int days, int hours, 
        int minutes, int seconds, int millisec )
    {
        TimeSpan elapsedTime = new TimeSpan( 
            days, hours, minutes, seconds, millisec );

        // Format the constructor for display.
        string ctor = 
            String.Format( "TimeSpan( {0}, {1}, {2}, {3}, {4} )", 
                days, hours, minutes, seconds, millisec);

        // Display the constructor and its value.
        Console.WriteLine( "{0,-48}{1,24}", 
            ctor, elapsedTime.ToString( ) );
    }

    static void Main( )
    {
        Console.WriteLine( 
            "This example of the " +
            "TimeSpan( int, int, int, int, int ) " +
            "\nconstructor generates the following output.\n" );
        Console.WriteLine( "{0,-48}{1,16}", "Constructor", "Value" );
        Console.WriteLine( "{0,-48}{1,16}", "-----------", "-----" );

        CreateTimeSpan( 10, 20, 30, 40, 50 );
        CreateTimeSpan( -10, 20, 30, 40, 50 );
        CreateTimeSpan( 0, 0, 0, 0, 937840050 );
        CreateTimeSpan( 1111, 2222, 3333, 4444, 5555 );
        CreateTimeSpan( 1111, -2222, -3333, -4444, -5555 );
        CreateTimeSpan( 99999, 99999, 99999, 99999, 99999 );
    } 
} 

/*
This example of the TimeSpan( int, int, int, int, int )
constructor generates the following output.

Constructor                                                Value
-----------                                                -----
TimeSpan( 10, 20, 30, 40, 50 )                       10.20:30:40.0500000
TimeSpan( -10, 20, 30, 40, 50 )                      -9.03:29:19.9500000
TimeSpan( 0, 0, 0, 0, 937840050 )                    10.20:30:40.0500000
TimeSpan( 1111, 2222, 3333, 4444, 5555 )           1205.22:47:09.5550000
TimeSpan( 1111, -2222, -3333, -4444, -5555 )       1016.01:12:50.4450000
TimeSpan( 99999, 99999, 99999, 99999, 99999 )    104236.05:27:18.9990000
*/

[C++] 
// Example of the TimeSpan( int, int, int, int, int ) constructor. 
#using <mscorlib.dll>
using namespace System;

// Create a TimeSpan object and display its value.
void CreateTimeSpan( int days, int hours, 
    int minutes, int seconds, int millisec )
{
    TimeSpan elapsedTime = 
        TimeSpan( days, hours, minutes, seconds, millisec );

    // Format the constructor for display.
    Object* boxedParams[ ] = __gc new Object*[ 5 ];
        boxedParams[ 0 ] = __box( days );
        boxedParams[ 1 ] = __box( hours );
        boxedParams[ 2 ] = __box( minutes );
        boxedParams[ 3 ] = __box( seconds );
        boxedParams[ 4 ] = __box( millisec );
    String* ctor = 
        String::Format( S"TimeSpan( {0}, {1}, {2}, {3}, {4} )", 
            boxedParams );

    // Display the constructor and its value.
    Console::WriteLine( S"{0,-48}{1,24}", 
        ctor, elapsedTime.ToString( ) );
}

void main( )
{
    Console::WriteLine( 
        S"This example of the TimeSpan( int, int, int, int, int ) " 
        S"\nconstructor generates the following output.\n" );
    Console::WriteLine(S"{0,-48}{1,16}", S"Constructor", S"Value" );
    Console::WriteLine(S"{0,-48}{1,16}", S"-----------", S"-----" );

    CreateTimeSpan( 10, 20, 30, 40, 50 );
    CreateTimeSpan( -10, 20, 30, 40, 50 );
    CreateTimeSpan( 0, 0, 0, 0, 937840050 );
    CreateTimeSpan( 1111, 2222, 3333, 4444, 5555 );
    CreateTimeSpan( 1111, -2222, -3333, -4444, -5555 );
    CreateTimeSpan( 99999, 99999, 99999, 99999, 99999 );
} 

/*
This example of the TimeSpan( int, int, int, int, int )
constructor generates the following output.

Constructor                                                Value
-----------                                                -----
TimeSpan( 10, 20, 30, 40, 50 )                       10.20:30:40.0500000
TimeSpan( -10, 20, 30, 40, 50 )                      -9.03:29:19.9500000
TimeSpan( 0, 0, 0, 0, 937840050 )                    10.20:30:40.0500000
TimeSpan( 1111, 2222, 3333, 4444, 5555 )           1205.22:47:09.5550000
TimeSpan( 1111, -2222, -3333, -4444, -5555 )       1016.01:12:50.4450000
TimeSpan( 99999, 99999, 99999, 99999, 99999 )    104236.05:27:18.9990000
*/

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

TimeSpan Structure | TimeSpan Members | System Namespace

Show: