Udostępnij za pośrednictwem


TimeSpan Constructor (Int32, Int32, Int32, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

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

Parameters

Exceptions

Exception Condition
ArgumentOutOfRangeException

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

Remarks

The specified days, hours, minutes, and seconds 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, and seconds.

' Example of the
' TimeSpan( 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)

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

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

      ' Display the constructor and its value.
      outputBlock.Text &= "{0,-44}{1,16}", _
          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 ) " & vbCrLf & _
          "constructor generates the following output." & vbCrLf & vbCrLf
      outputBlock.Text &= String.Format("{0,-44}{1,16}", "Constructor", "Value") & vbCrLf
      outputBlock.Text &= String.Format("{0,-44}{1,16}", "-----------", "-----") & vbCrLf

      CreateTimeSpan(outputBlock, 10, 20, 30, 40)
      CreateTimeSpan(outputBlock, -10, 20, 30, 40)
      CreateTimeSpan(outputBlock, 0, 0, 0, 937840)
      CreateTimeSpan(outputBlock, 1000, 2000, 3000, 4000)
      CreateTimeSpan(outputBlock, 1000, -2000, -3000, -4000)
      CreateTimeSpan(outputBlock, 999999, 999999, 999999, 999999)
   End Sub
End Module

' This example of the
' TimeSpan( Integer, Integer, Integer, Integer )
' constructor generates the following output.
' 
' Constructor                                            Value
' -----------                                            -----
' TimeSpan( 10, 20, 30, 40 )                       10.20:30:40
' TimeSpan( -10, 20, 30, 40 )                      -9.03:29:20
' TimeSpan( 0, 0, 0, 937840 )                      10.20:30:40
' TimeSpan( 1000, 2000, 3000, 4000 )             1085.11:06:40
' TimeSpan( 1000, -2000, -3000, -4000 )           914.12:53:20
' TimeSpan( 999999, 999999, 999999, 999999 )   992661.08:57:23
// Example of the TimeSpan( 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)
   {
      TimeSpan elapsedTime =
          new TimeSpan(days, hours, minutes, seconds);

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

      // Display the constructor and its value.
      outputBlock.Text += "{0,-44}{1,16}",
          ctor, elapsedTime.ToString() + "\n";
   }

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

      CreateTimeSpan(outputBlock, 10, 20, 30, 40);
      CreateTimeSpan(outputBlock, -10, 20, 30, 40);
      CreateTimeSpan(outputBlock, 0, 0, 0, 937840);
      CreateTimeSpan(outputBlock, 1000, 2000, 3000, 4000);
      CreateTimeSpan(outputBlock, 1000, -2000, -3000, -4000);
      CreateTimeSpan(outputBlock, 999999, 999999, 999999, 999999);
   }
}

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

Constructor                                            Value
-----------                                            -----
TimeSpan( 10, 20, 30, 40 )                       10.20:30:40
TimeSpan( -10, 20, 30, 40 )                      -9.03:29:20
TimeSpan( 0, 0, 0, 937840 )                      10.20:30:40
TimeSpan( 1000, 2000, 3000, 4000 )             1085.11:06:40
TimeSpan( 1000, -2000, -3000, -4000 )           914.12:53:20
TimeSpan( 999999, 999999, 999999, 999999 )   992661.08:57:23
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.