Partager via


TimeSpan.FromTicks Method

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

Returns a TimeSpan that represents a specified time, where the specification is in units of ticks.

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

Syntax

'Declaration
Public Shared Function FromTicks ( _
    value As Long _
) As TimeSpan
public static TimeSpan FromTicks(
    long value
)

Parameters

  • value
    Type: System.Int64
    A number of ticks that represent a time.

Return Value

Type: System.TimeSpan
An object that represents value.

Remarks

This is a convenience method with the same behavior as the TimeSpan.TimeSpan(Int64) constructor.

Examples

The following code example creates several TimeSpan objects using the FromTicks method.

' Example of the TimeSpan.FromTicks( Long ) method.

Module Example

   Sub GenTimeSpanFromTicks(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal ticks As Long)

      ' Create a TimeSpan object and TimeSpan string from 
      ' a number of ticks.
      Dim interval As TimeSpan = TimeSpan.FromTicks(ticks)
      Dim timeInterval As String = interval.ToString()

      ' Pad the end of the TimeSpan string with spaces if it 
      ' does not contain milliseconds.
      Dim pIndex As Integer = timeInterval.IndexOf(":"c)
      pIndex = timeInterval.IndexOf("."c, pIndex)
      If pIndex < 0 Then timeInterval &= "        "

      outputBlock.Text &= String.Format("{0,21}{1,26}", ticks, timeInterval) & vbCrLf
   End Sub

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

      outputBlock.Text &= _
          "This example of TimeSpan.FromTicks( Long )" & _
          vbCrLf & "generates the following output." & vbCrLf & vbCrLf
      outputBlock.Text &= "{0,21}{1,18}", _
          "FromTicks", "TimeSpan" & vbCrLf
      outputBlock.Text &= "{0,21}{1,18}", _
          "---------", "--------" & vbCrLf

      GenTimeSpanFromTicks(outputBlock, 1)
      GenTimeSpanFromTicks(outputBlock, 12345)
      GenTimeSpanFromTicks(outputBlock, 123456789)
      GenTimeSpanFromTicks(outputBlock, 1234567898765)
      GenTimeSpanFromTicks(outputBlock, 12345678987654321)
      GenTimeSpanFromTicks(outputBlock, 10000000)
      GenTimeSpanFromTicks(outputBlock, 600000000)
      GenTimeSpanFromTicks(outputBlock, 36000000000)
      GenTimeSpanFromTicks(outputBlock, 864000000000)
      GenTimeSpanFromTicks(outputBlock, 18012202000000)
   End Sub
End Module

' This example of TimeSpan.FromTicks( Long )
' generates the following output.
' 
'             FromTicks          TimeSpan
'             ---------          --------
'                     1          00:00:00.0000001
'                 12345          00:00:00.0012345
'             123456789          00:00:12.3456789
'         1234567898765        1.10:17:36.7898765
'     12345678987654321    14288.23:31:38.7654321
'              10000000          00:00:01
'             600000000          00:01:00
'           36000000000          01:00:00
'          864000000000        1.00:00:00
'        18012202000000       20.20:20:20.2000000
// Example of the TimeSpan.FromTicks( long ) method.
using System;

class Example
{
   static void GenTimeSpanFromTicks(System.Windows.Controls.TextBlock outputBlock, long ticks)
   {
      // Create a TimeSpan object and TimeSpan string from 
      // a number of ticks.
      TimeSpan interval = TimeSpan.FromTicks(ticks);
      string timeInterval = interval.ToString();

      // Pad the end of the TimeSpan string with spaces if it 
      // does not contain milliseconds.
      int pIndex = timeInterval.IndexOf(':');
      pIndex = timeInterval.IndexOf('.', pIndex);
      if (pIndex < 0) timeInterval += "        ";

      outputBlock.Text += String.Format("{0,21}{1,26}", ticks, timeInterval) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += 
          "This example of TimeSpan.FromTicks( long )\n" +
          "generates the following output.\n" + "\n";
      outputBlock.Text += "{0,21}{1,18}",
          "FromTicks", "TimeSpan" + "\n";
      outputBlock.Text += "{0,21}{1,18}",
          "---------", "--------" + "\n";

      GenTimeSpanFromTicks(outputBlock, 1);
      GenTimeSpanFromTicks(outputBlock, 12345);
      GenTimeSpanFromTicks(outputBlock, 123456789);
      GenTimeSpanFromTicks(outputBlock, 1234567898765);
      GenTimeSpanFromTicks(outputBlock, 12345678987654321);
      GenTimeSpanFromTicks(outputBlock, 10000000);
      GenTimeSpanFromTicks(outputBlock, 600000000);
      GenTimeSpanFromTicks(outputBlock, 36000000000);
      GenTimeSpanFromTicks(outputBlock, 864000000000);
      GenTimeSpanFromTicks(outputBlock, 18012202000000);
   }
}

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

            FromTicks          TimeSpan
            ---------          --------
                    1          00:00:00.0000001
                12345          00:00:00.0012345
            123456789          00:00:12.3456789
        1234567898765        1.10:17:36.7898765
    12345678987654321    14288.23:31:38.7654321
             10000000          00:00:01
            600000000          00:01:00
          36000000000          01:00:00
         864000000000        1.00:00:00
       18012202000000       20.20:20:20.2000000
*/

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.