Updated: May 2010
Gets the number of ticks that represent the date and time of this instance.
Assembly: mscorlib (in mscorlib.dll)
Public ReadOnly Property Ticks As Long
public long Ticks { get; }
public: property long long Ticks { long long get (); }
member Ticks : int64
Property Value
Type: System.Int64The number of ticks that represent the date and time of this instance. The value is between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks.
A single tick represents one hundred nanoseconds or one ten-millionth of a second. There are 10,000 ticks in a millisecond.
The value of this property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight, January 1, 0001, which represents DateTime.MinValue. It does not include the number of ticks that are attributable to leap seconds.
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. The TimeSpan object is then used to display the elapsed time using several other time intervals.
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
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); // If run on December 14, 2007, at 15:23, this example displays the // following output to the console: // Elapsed from the beginning of the century to Friday, December 14, 2007 3:23 PM: // 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
.NET Framework
Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0.NET Framework Client Profile
Supported in: 4, 3.5 SP1Portable Class Library
Supported in: Portable Class LibraryWindows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Reference
|
Date |
History |
Reason |
|---|---|---|
|
May 2010 |
Added a remark about leap seconds. |
Customer feedback. |
DateTime cannot be used for fast time operations because a new class must be constructed to represent each value you want to work with.
Even the Add/Subtract operations return a new object. So in order to check e.g. the day name on a bunch of longs representing dates, we have to allocate and clean up space for every single check, rather than simply running the values into a single object and performing the checks there.
I can only imagine you had some reason for making all the fields readonly, something related to making .NET4 'designed by grandma' or something, which has zero relevance to usage scenarios of real programmers?
---
Hi Freqy, DateTime is a structure and thus a value type, not a reference type. So unless you pass around a DateTime as an object, which would box its value, then the DateTime will stay on the call stack.
DateTime objUTC = DateTime.Now.ToUniversalTime();
long epoch = (objUTC.Ticks - 621355968000000000) / 10000;
Converting back is simple mathematics...
DateTime objDate = new DateTime(((epoch * 10000000) + 621355968000000000));
For further conversions.. follow this link. http://www.epochconverter.com/
Hope this will help.
Regards.