Udostępnij za pośrednictwem


TimeSpan Constructor (Int64)

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

Initializes a new TimeSpan to the specified number of ticks.

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

Syntax

'Declaration
Public Sub New ( _
    ticks As Long _
)
public TimeSpan(
    long ticks
)

Parameters

  • ticks
    Type: System.Int64
    A time period expressed in 100-nanosecond units.

Examples

The following code example creates several TimeSpan objects using the constructor overload that initializes a TimeSpan to a specified number of ticks.

' Example of the TimeSpan( Long ) constructor.

Module Example

   ' Create a TimeSpan object and display its value.
   Sub CreateTimeSpan(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal ticks As Long)

      Dim elapsedTime As New TimeSpan(ticks)

      ' Format the constructor for display.
      Dim ctor As String = _
          String.Format("TimeSpan( {0} )", ticks)

      ' Pad the end of a TimeSpan string with spaces if
      ' it does not contain milliseconds.
      Dim elapsedStr As String = elapsedTime.ToString()
      Dim pointIndex As Integer = elapsedStr.IndexOf(":"c)

      pointIndex = elapsedStr.IndexOf("."c, pointIndex)
      If pointIndex < 0 Then elapsedStr &= "        "

      ' Display the constructor and its value.
      outputBlock.Text &= String.Format("{0,-33}{1,24}", ctor, elapsedStr) & vbCrLf
   End Sub

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

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

      CreateTimeSpan(outputBlock, 1)
      CreateTimeSpan(outputBlock, 999999)
      CreateTimeSpan(outputBlock, -1000000000000)
      CreateTimeSpan(outputBlock, 18012202000000)
      CreateTimeSpan(outputBlock, 999999999999999999)
      CreateTimeSpan(outputBlock, 1000000000000000000)

   End Sub
End Module

' This example of the TimeSpan( Long ) constructor
' generates the following output.
' 
' Constructor                                 Value
' -----------                                 -----
' TimeSpan( 1 )                            00:00:00.0000001
' TimeSpan( 999999 )                       00:00:00.0999999
' TimeSpan( -1000000000000 )            -1.03:46:40
' TimeSpan( 18012202000000 )            20.20:20:20.2000000
' TimeSpan( 999999999999999999 )   1157407.09:46:39.9999999
' TimeSpan( 1000000000000000000 )  1157407.09:46:40
// Example of the TimeSpan( long ) constructor.
using System;

class Example
{
   // Create a TimeSpan object and display its value.
   static void CreateTimeSpan(System.Windows.Controls.TextBlock outputBlock, long ticks)
   {
      TimeSpan elapsedTime = new TimeSpan(ticks);

      // Format the constructor for display.
      string ctor = String.Format("TimeSpan( {0} )", ticks);

      // Pad the end of a TimeSpan string with spaces if
      // it does not contain milliseconds.
      string elapsedStr = elapsedTime.ToString();
      int pointIndex = elapsedStr.IndexOf(':');

      pointIndex = elapsedStr.IndexOf('.', pointIndex);
      if (pointIndex < 0) elapsedStr += "        ";

      // Display the constructor and its value.
      outputBlock.Text += String.Format("{0,-33}{1,24}", ctor, elapsedStr) + "\n";
   }

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

      CreateTimeSpan(outputBlock, 1);
      CreateTimeSpan(outputBlock, 999999);
      CreateTimeSpan(outputBlock, -1000000000000);
      CreateTimeSpan(outputBlock, 18012202000000);
      CreateTimeSpan(outputBlock, 999999999999999999);
      CreateTimeSpan(outputBlock, 1000000000000000000);
   }
}

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

Constructor                                 Value
-----------                                 -----
TimeSpan( 1 )                            00:00:00.0000001
TimeSpan( 999999 )                       00:00:00.0999999
TimeSpan( -1000000000000 )            -1.03:46:40
TimeSpan( 18012202000000 )            20.20:20:20.2000000
TimeSpan( 999999999999999999 )   1157407.09:46:39.9999999
TimeSpan( 1000000000000000000 )  1157407.09:46:40
*/

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.