Partager via


TimeSpan.Ticks Property

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

Gets the number of ticks that represents the value of the current TimeSpan structure.

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

Syntax

'Declaration
Public ReadOnly Property Ticks As Long
public long Ticks { get; }

Property Value

Type: System.Int64
The number of ticks contained in this instance.

Remarks

The smallest unit of time is the tick, which is equal to 100 nanoseconds. A tick can be negative or positive.

Examples

The following code example creates several TimeSpan objects and displays the Ticks property of each.

' Example of the TimeSpan class properties.

Module Example

   Const headerFmt As String = vbCrLf & "{0,-45}"
   Const dataFmt As String = "{0,-12}{1,8}       {2,-18}{3,21}"

   ' Display the properties of the TimeSpan parameter.
   Sub ShowTimeSpanProperties(ByVal outputBlock As System.Windows.Controls.TextBlock, ByVal interval As TimeSpan)

      outputBlock.Text += String.Format("{0,21}", interval) & vbCrLf
      outputBlock.Text += String.Format(dataFmt, _
          "Days", interval.Days, "TotalDays", interval.TotalDays) + vbCrLf
      outputBlock.Text += String.Format(dataFmt, "Hours", interval.Hours, _
          "TotalHours", interval.TotalHours) + vbCrLf
      outputBlock.Text += String.Format(dataFmt, "Minutes", interval.Minutes, _
          "TotalMinutes", interval.TotalMinutes) + vbCrLf
      outputBlock.Text += String.Format(dataFmt, "Seconds", interval.Seconds, _
          "TotalSeconds", interval.TotalSeconds) + vbCrLf
      outputBlock.Text += String.Format(dataFmt, _
          "Milliseconds", interval.Milliseconds, _
          "TotalMilliseconds", interval.TotalMilliseconds) + vbCrLf
      outputBlock.Text += String.Format(dataFmt, _
          Nothing, Nothing, "Ticks", interval.Ticks) + vbCrLf
   End Sub

   Public Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      outputBlock.Text += _
          "This example of the TimeSpan class properties " & _
          "generates the " & vbCrLf & "following output. It " & _
          "creates several TimeSpan objects and " & vbCrLf & _
          "displays the values of the TimeSpan properties for " & _
          "each." + vbCrLf

      ' Create and display a TimeSpan value of 1 tick.
      outputBlock.Text += String.Format(headerFmt, "TimeSpan( 1 )")
      ShowTimeSpanProperties(outputBlock, New TimeSpan(1))

      ' Create a TimeSpan value with a large number of ticks.
      outputBlock.Text += String.Format(headerFmt, "TimeSpan( 111222333444555 )")
      ShowTimeSpanProperties(outputBlock, New TimeSpan(111222333444555))

      ' This TimeSpan has all fields specified.
      outputBlock.Text += String.Format(headerFmt, "TimeSpan( 10, 20, 30, 40, 50 )")
      ShowTimeSpanProperties(outputBlock, New TimeSpan(10, 20, 30, 40, 50))

      ' This TimeSpan has all fields overflowing.
      outputBlock.Text += String.Format(headerFmt, _
          "TimeSpan( 1111, 2222, 3333, 4444, 5555 )")
      ShowTimeSpanProperties(outputBlock,  _
          New TimeSpan(1111, 2222, 3333, 4444, 5555))

      ' This TimeSpan is based on a number of days.
      outputBlock.Text += String.Format(headerFmt, "FromDays( 20.84745602 )")
      ShowTimeSpanProperties(outputBlock, TimeSpan.FromDays(20.84745602))
   End Sub
End Module

' This example of the TimeSpan class properties generates the
' following output. It creates several TimeSpan objects and
' displays the values of the TimeSpan properties for each.
' 
' TimeSpan( 1 )                                     00:00:00.0000001
' Days               0       TotalDays          1.15740740740741E-12
' Hours              0       TotalHours         2.77777777777778E-11
' Minutes            0       TotalMinutes       1.66666666666667E-09
' Seconds            0       TotalSeconds                      1E-07
' Milliseconds       0       TotalMilliseconds                0.0001
'                            Ticks                                 1
' 
' TimeSpan( 111222333444555 )                   128.17:30:33.3444555
' Days             128       TotalDays              128.729552597865
' Hours             17       TotalHours             3089.50926234875
' Minutes           30       TotalMinutes           185370.555740925
' Seconds           33       TotalSeconds           11122233.3444555
' Milliseconds     344       TotalMilliseconds      11122233344.4555
'                            Ticks                   111222333444555
' 
' TimeSpan( 10, 20, 30, 40, 50 )                 10.20:30:40.0500000
' Days              10       TotalDays              10.8546302083333
' Hours             20       TotalHours                   260.511125
' Minutes           30       TotalMinutes                 15630.6675
' Seconds           40       TotalSeconds                  937840.05
' Milliseconds      50       TotalMilliseconds             937840050
'                            Ticks                     9378400500000
' 
' TimeSpan( 1111, 2222, 3333, 4444, 5555 )     1205.22:47:09.5550000
' Days            1205       TotalDays              1205.94941614583
' Hours             22       TotalHours                28942.7859875
' Minutes           47       TotalMinutes              1736567.15925
' Seconds            9       TotalSeconds              104194029.555
' Milliseconds     555       TotalMilliseconds          104194029555
'                            Ticks                  1041940295550000
' 
' FromDays( 20.84745602 )                        20.20:20:20.2000000
' Days              20       TotalDays              20.8474560185185
' Hours             20       TotalHours             500.338944444444
' Minutes           20       TotalMinutes           30020.3366666667
' Seconds           20       TotalSeconds                  1801220.2
' Milliseconds     200       TotalMilliseconds            1801220200
'                            Ticks                    18012202000000
// Example of the TimeSpan class properties.
using System;

class Example
{
   const string headerFmt = "\n{0,-45}";
   const string dataFmt = "{0,-12}{1,8}       {2,-18}{3,21}";

   // Display the properties of the TimeSpan parameter.
   static void ShowTimeSpanProperties(System.Windows.Controls.TextBlock outputBlock, TimeSpan interval)
   {
      outputBlock.Text += String.Format("{0,21}", interval) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Days", interval.Days,
          "TotalDays", interval.TotalDays) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Hours", interval.Hours,
          "TotalHours", interval.TotalHours) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Minutes", interval.Minutes,
          "TotalMinutes", interval.TotalMinutes) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Seconds", interval.Seconds,
          "TotalSeconds", interval.TotalSeconds) + "\n";
      outputBlock.Text += String.Format(dataFmt, "Milliseconds",
          interval.Milliseconds, "TotalMilliseconds",
          interval.TotalMilliseconds) + "\n";
      outputBlock.Text += String.Format(dataFmt, null, null,
          "Ticks", interval.Ticks) + "\n";
   }

   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      outputBlock.Text += 
          "This example of the TimeSpan class properties " +
          "generates the \nfollowing output. It " +
          "creates several TimeSpan objects and \ndisplays " +
          "the values of the TimeSpan properties for each." + "\n";

      // Create and display a TimeSpan value of 1 tick.
      outputBlock.Text += String.Format(headerFmt, "TimeSpan( 1 )");
      ShowTimeSpanProperties(outputBlock, new TimeSpan(1));

      // Create a TimeSpan value with a large number of ticks.
      outputBlock.Text += String.Format(headerFmt, "TimeSpan( 111222333444555 )");
      ShowTimeSpanProperties(outputBlock, new TimeSpan(111222333444555));

      // This TimeSpan has all fields specified.
      outputBlock.Text += String.Format(headerFmt, "TimeSpan( 10, 20, 30, 40, 50 )");
      ShowTimeSpanProperties(outputBlock, new TimeSpan(10, 20, 30, 40, 50));

      // This TimeSpan has all fields overflowing.
      outputBlock.Text += String.Format(headerFmt,
          "TimeSpan( 1111, 2222, 3333, 4444, 5555 )");
      ShowTimeSpanProperties(outputBlock, 
          new TimeSpan(1111, 2222, 3333, 4444, 5555));

      // This TimeSpan is based on a number of days.
      outputBlock.Text += String.Format(headerFmt, "FromDays( 20.84745602 )");
      ShowTimeSpanProperties(outputBlock, TimeSpan.FromDays(20.84745602));
   }
}

/*
This example of the TimeSpan class properties generates the
following output. It creates several TimeSpan objects and
displays the values of the TimeSpan properties for each.

TimeSpan( 1 )                                     00:00:00.0000001
Days               0       TotalDays          1.15740740740741E-12
Hours              0       TotalHours         2.77777777777778E-11
Minutes            0       TotalMinutes       1.66666666666667E-09
Seconds            0       TotalSeconds                      1E-07
Milliseconds       0       TotalMilliseconds                0.0001
                           Ticks                                 1

TimeSpan( 111222333444555 )                   128.17:30:33.3444555
Days             128       TotalDays              128.729552597865
Hours             17       TotalHours             3089.50926234875
Minutes           30       TotalMinutes           185370.555740925
Seconds           33       TotalSeconds           11122233.3444555
Milliseconds     344       TotalMilliseconds      11122233344.4555
                           Ticks                   111222333444555

TimeSpan( 10, 20, 30, 40, 50 )                 10.20:30:40.0500000
Days              10       TotalDays              10.8546302083333
Hours             20       TotalHours                   260.511125
Minutes           30       TotalMinutes                 15630.6675
Seconds           40       TotalSeconds                  937840.05
Milliseconds      50       TotalMilliseconds             937840050
                           Ticks                     9378400500000

TimeSpan( 1111, 2222, 3333, 4444, 5555 )     1205.22:47:09.5550000
Days            1205       TotalDays              1205.94941614583
Hours             22       TotalHours                28942.7859875
Minutes           47       TotalMinutes              1736567.15925
Seconds            9       TotalSeconds              104194029.555
Milliseconds     555       TotalMilliseconds          104194029555
                           Ticks                  1041940295550000

FromDays( 20.84745602 )                        20.20:20:20.2000000
Days              20       TotalDays              20.8474560185185
Hours             20       TotalHours             500.338944444444
Minutes           20       TotalMinutes           30020.3366666667
Seconds           20       TotalSeconds                  1801220.2
Milliseconds     200       TotalMilliseconds            1801220200
                           Ticks                    18012202000000
*/

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.