共用方式為


DateTime.Ticks 屬性

定義

取得表示這個執行個體日期和時間的刻度數目。

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

屬性值

刻度數目,表示這個執行個體的日期和時間。 值介於 DateTime.MinValue.TicksDateTime.MaxValue.Ticks 之間。

範例

下列範例會 Ticks 使用 屬性來顯示自 201 年開始經過的刻度數目,並具現化 TimeSpan 物件。 然後,物件 TimeSpan 會使用數個其他時間間隔來顯示經過的時間。

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
open System

let centuryBegin = DateTime(2001, 1, 1)
let currentDate = DateTime.Now

let elapsedTicks = currentDate.Ticks - centuryBegin.Ticks
let elapsedSpan = TimeSpan elapsedTicks

printfn $"Elapsed from the beginning of the century to {currentDate:f}:"
printfn $"   {elapsedTicks * 100L:N0} nanoseconds"
printfn $"   {elapsedTicks:N0} ticks"
printfn $"   {elapsedSpan.TotalSeconds:N2} seconds"
printfn $"   {elapsedSpan.TotalMinutes:N2} minutes"
printfn $"   {elapsedSpan.Days:N0} days, {elapsedSpan.Hours} hours, {elapsedSpan.Minutes} minutes, {elapsedSpan.Seconds} 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

備註

一刻度代表 100 毫微秒,即一千萬分之一秒。 毫秒內有 10,000 個刻度, (查看 TicksPerMillisecond 每秒) 和 1000 萬個刻度。

此屬性的值代表西曆中自 12:00:00:00 午夜 00:00 起經過的 100 奈秒間隔數目,代表 MinValue 西曆的 0001 年 1 月 1 日。 它不包含可達到閏秒的刻度數目。 如果 DateTime 物件的 Kind 屬性設定 Local 為 ,則其刻度代表從 12:00:00 午夜到 0001 年 1 月 1 日于當地時間所指定的時間經過時間,如目前時區設定所指定。 DateTime如果物件的 Kind 屬性設定為 Utc ,則其刻度代表國際標準時間中自 12:00:00 午夜 00:00 起經過的時間。 DateTime如果物件的 Kind 屬性設定為 Unspecified ,則其刻度代表未知時區中自 12:00:00 午夜 00:00 起經過的時間。

一般而言,刻度會根據 屬性所 Kind 指定的時區來表示時間。

適用於