Condividi tramite


DateTimeOffset.Ticks Property

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

Updated: May 2010

Gets the number of ticks that represents the date and time of the current DateTimeOffset object in clock time.

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 in the DateTimeOffset object's clock time.

Remarks

The Ticks property is not affected by the value of the Offset property.

The value of the Ticks property represents the number of 100-nanosecond intervals that have elapsed since 12:00:00 midnight on January 1, 0001 (the value of MinValue). It does not include ticks that would be added by leap seconds. A nanosecond is one billionth of a second; there are ten million ticks in a second. The value of the Ticks property ranges from DateTimeOffset.MinValue.Ticks to DateTimeOffset.MaxValue.Ticks.

You can assign the number of ticks to a DateTimeOffset object by using the DateTimeOffset(Int64, TimeSpan) constructor overload.

Examples

The following example initializes a DateTimeOffset object by approximating the number of ticks in the date July 1, 2008 1:23:07. It then displays the date and the number of ticks represented by that date to the console.

' Attempt to initialize date to number of ticks
' in July 1, 2008 1:23:07.      
'
' There are 10,000,000 100-nanosecond intervals in a second
Const NSPerSecond As Long = 10000000
Dim ticks As Long
ticks = 7 * NSPerSecond                         ' Ticks in a 7 seconds 
ticks += 23 * 60 * NSPerSecond                  ' Ticks in 23 minutes
ticks += 1 * 60 * 60 * NSPerSecond              ' Ticks in 1 hour
ticks += 60 * 60 * 24 * NSPerSecond             ' Ticks in 1 day
ticks += 181 * 60 * 60 * 24 * NSPerSecond       ' Ticks in 6 months 
ticks += 2007 * 60 * 60 * 24 * 365L * NSPerSecond   ' Ticks in 2007 years 
ticks += 486 * 60 * 60 * 24 * NSPerSecond       ' Adjustment for leap years      
Dim dto As DateTimeOffset = New DateTimeOffset( _
                            ticks, _
                            DateTimeOffset.Now.Offset)
outputBlock.Text &= String.Format("There are {0:n0} ticks in {1}.", _
                  dto.Ticks, _
                  dto.ToString()) & vbCrLf
// Attempt to initialize date to number of ticks
// in July 1, 2008 1:23:07.      
//
// There are 10,000,000 100-nanosecond intervals in a second
const long NSPerSecond = 10000000;
long ticks;
ticks = 7 * NSPerSecond;                        // Ticks in a 7 seconds 
ticks += 23 * 60 * NSPerSecond;                 // Ticks in 23 minutes
ticks += 1 * 60 * 60 * NSPerSecond;             // Ticks in 1 hour
ticks += 60 * 60 * 24 * NSPerSecond;            // Ticks in 1 day
ticks += 181 * 60 * 60 * 24 * NSPerSecond;      // Ticks in 6 months 
ticks += 2007 * 60 * 60 * 24 * 365L * NSPerSecond;   // Ticks in 2007 years 
ticks += 486 * 60 * 60 * 24 * NSPerSecond;      // Adjustment for leap years      
DateTimeOffset dto = new DateTimeOffset(
                         ticks,
                         DateTimeOffset.Now.Offset);
outputBlock.Text += String.Format("There are {0:n0} ticks in {1}.",
                  dto.Ticks,
                  dto.ToString()) + "\n";

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.

Change History

Date

History

Reason

May 2010

Added a remark about leap seconds.

Customer feedback.