TimeSpan Constructor (Int32, Int32, Int32, Int32)

July 28, 2014

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

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

public TimeSpan(
	int days,
	int hours,
	int minutes,
	int seconds
)

Parameters

days
Type: System.Int32
Number of days.
hours
Type: System.Int32
Number of hours.
minutes
Type: System.Int32
Number of minutes.
seconds
Type: System.Int32
Number of seconds.

ExceptionCondition
ArgumentOutOfRangeException

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

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

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.


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 += 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);
   }
}
// The example displays 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 )  1042371.15:25:39


Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

Show:
© 2014 Microsoft