TimeSpan Constructor (Int32, Int32, Int32, Int32, Int32)

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

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

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

Syntax

Public Sub New ( _
    days As Integer, _
    hours As Integer, _
    minutes As Integer, _
    seconds As Integer, _
    milliseconds As Integer _
)
public TimeSpan(
    int days,
    int hours,
    int minutes,
    int seconds,
    int milliseconds
)

Parameters

Exceptions

Exception Condition
ArgumentOutOfRangeException

The parameters specify a TimeSpan value less than MinValue or greater than MaxValue.

Remarks

The specified days, hours, minutes, seconds, and milliseconds are converted to ticks, and that value initializes this instance.

Examples

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.

' Example of the 
' TimeSpan( Integer, Integer, Integer, Integer, Integer ) constructor. 

Module Example

   ' Create a TimeSpan object and display its value.
   Sub CreateTimeSpan(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal days As Integer, ByVal hours As Integer, _
       ByVal minutes As Integer, ByVal seconds As Integer, ByVal 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.
      outputBlock.Text &= "{0,-48}{1,24}", _
          ctor, elapsedTime.ToString() & vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)

      outputBlock.Text &= _
          "This example of the " & vbCrLf & _
          "TimeSpan( Integer, Integer, " & _
          "Integer, Integer, Integer ) " & vbCrLf & _
          "constructor generates the following output." & vbCrLf & vbCrLf
      outputBlock.Text &= String.Format("{0,-48}{1,16}", "Constructor", "Value") & vbCrLf
      outputBlock.Text &= String.Format("{0,-48}{1,16}", "-----------", "-----") & vbCrLf

      CreateTimeSpan(outputBlock, 10, 20, 30, 40, 50)
      CreateTimeSpan(outputBlock, -10, 20, 30, 40, 50)
      CreateTimeSpan(outputBlock, 0, 0, 0, 0, 937840050)
      CreateTimeSpan(outputBlock, 1111, 2222, 3333, 4444, 5555)
      CreateTimeSpan(outputBlock, 1111, -2222, -3333, -4444, -5555)
      CreateTimeSpan(outputBlock, 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
// Example of the TimeSpan( int, int, int, int, int ) constructor. 
using System;

class Example
{
   // Create a TimeSpan object and display its value.
   static void CreateTimeSpan(System.Windows.Controls.TextBlock outputBlock, 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.
      outputBlock.Text += "{0,-48}{1,24}",
          ctor, elapsedTime.ToString() + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text +=
          "This example of the " +
          "TimeSpan( int, int, int, int, int ) " +
          "\nconstructor generates the following output.\n" + "\n";
      outputBlock.Text += String.Format("{0,-48}{1,16}", "Constructor", "Value") + "\n";
      outputBlock.Text += String.Format("{0,-48}{1,16}", "-----------", "-----") + "\n";

      CreateTimeSpan(outputBlock, 10, 20, 30, 40, 50);
      CreateTimeSpan(outputBlock, -10, 20, 30, 40, 50);
      CreateTimeSpan(outputBlock, 0, 0, 0, 0, 937840050);
      CreateTimeSpan(outputBlock, 1111, 2222, 3333, 4444, 5555);
      CreateTimeSpan(outputBlock, 1111, -2222, -3333, -4444, -5555);
      CreateTimeSpan(outputBlock, 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
*/

Version Information

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Platforms

Windows Phone

See Also

Reference

TimeSpan Structure

TimeSpan Overload

System Namespace

Int64