DateTime.Ticks 属性

定义

获取表示此实例的日期和时间的计时周期数。Gets the number of ticks that represent the date and time of this instance.

public:
 property long Ticks { long get(); };
public long Ticks { get; }
member this.Ticks : int64
Public ReadOnly Property Ticks As Long

属性值

表示此实例的日期和时间的计时周期数。The number of ticks that represent the date and time of this instance. 该值介于 DateTime.MinValue.TicksDateTime.MaxValue.Ticks 之间。The value is between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.

示例

备注

The C# examples in this article run in the Try.NET inline code runner and playground. Select the Run button to run an example in an interactive window. Once you execute the code, you can modify it and run the modified code by selecting Run again. The modified code either runs in the interactive window or, if compilation fails, the interactive window displays all C# compiler error messages.

下面的示例使用 Ticks 属性来显示自第二十五个世纪开始后经过的计时周期数,并实例化一个 TimeSpan 对象。The following example uses the Ticks property to display the number of ticks that have elapsed since the beginning of the twenty-first century and to instantiate a TimeSpan object. 然后,使用 TimeSpan 对象显示使用几个其他时间间隔的运行时间。The TimeSpan object is then used to display the elapsed time using several other time intervals.

DateTime centuryBegin = new DateTime(2001, 1, 1);
DateTime currentDate = DateTime.Now;

long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks;
TimeSpan elapsedSpan = new TimeSpan(elapsedTicks);

Console.WriteLine("Elapsed from the beginning of the century to {0:f}:", 
                   currentDate);
Console.WriteLine("   {0:N0} nanoseconds", elapsedTicks * 100);
Console.WriteLine("   {0:N0} ticks", elapsedTicks);
Console.WriteLine("   {0:N2} seconds", elapsedSpan.TotalSeconds);
Console.WriteLine("   {0:N2} minutes", elapsedSpan.TotalMinutes);
Console.WriteLine("   {0:N0} days, {1} hours, {2} minutes, {3} seconds", 
                  elapsedSpan.Days, elapsedSpan.Hours, 
                  elapsedSpan.Minutes, elapsedSpan.Seconds);

// This example displays an output similar to the following:
// 
// Elapsed from the beginning of the century to Thursday, 14 November 2019 18:21:
//    595,448,498,171,000,000 nanoseconds
//    5,954,484,981,710,000 ticks
//    595,448,498.17 seconds
//    9,924,141.64 minutes
//    6,891 days, 18 hours, 21 minutes, 38 seconds
Dim centuryBegin As Date = #1/1/2001 0:0:0#
Dim currentDate As Date = Date.Now
Dim elapsedTicks As Long = currentDate.Ticks - centuryBegin.Ticks
Dim elapsedSpan As New TimeSpan(elapsedTicks)

Console.WriteLine("Elapsed from the beginning of the century to {0:f}:", _
                   currentDate)
Console.WriteLine("   {0:N0} nanoseconds", elapsedTicks * 100)
Console.WriteLine("   {0:N0} ticks", elapsedTicks)
Console.WriteLine("   {0:N2} seconds", elapsedSpan.TotalSeconds)
Console.WriteLine("   {0:N2} minutes", elapsedSpan.TotalMinutes)
Console.WriteLine("   {0:N0} days, {1} hours, {2} minutes, {3} seconds", _
                  elapsedSpan.Days, elapsedSpan.Hours, _
                  elapsedSpan.Minutes, elapsedSpan.Seconds)
' If run on December 14, 2007, at 15:23, this example displays the
' following output to the console:
'          219,338,580,000,000,000 nanoseconds
'          2,193,385,800,000,000 ticks
'          219,338,580.00 seconds
'          3,655,643.00 minutes
'          2,538 days, 15 hours, 23 minutes, 0 seconds

注解

一个计时周期表示一百纳秒,即一千万分之一秒。A single tick represents one hundred nanoseconds or one ten-millionth of a second. 1 毫秒内有 10,000 个计时周期,即 1 秒内有 1,000 万个计时周期。There are 10,000 ticks in a millisecond, or 10 million ticks in a second.

此属性的值表示自0001年1月1日午夜12月1日午夜 12:00:00 0:00:00 (公历日期为0001年1月1日)的100纳秒间隔数,表示 DateTime.MinValueThe value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001 (0:00:00 UTC on January 1, 0001, in the Gregorian calendar), which represents DateTime.MinValue. 它不包括由闰秒组成的计时周期数。It does not include the number of ticks that are attributable to leap seconds.

适用于