DateTimeOffset::Ticks 属性

http://msdn.microsoft.com/zh-cn/library/system.datetimeoffset.ticks(VS.95).aspx

 

获取计时周期数,此计时周期数表示时钟时间中当前 DateTimeOffset 对象的日期和时间。

 

Ticks 属性不受 Offset 属性值的影响。

Ticks 属性的值表示自 0001 年 1 月 1 日午夜 12:00:00 以来所经历的以 100 纳秒为间隔的间隔数(MinValue 的值。)。一纳秒等于十亿分之一秒;一秒钟包含一千万个计时周期。Ticks 属性的值介于 DateTimeOffset.MinValue.TicksDateTimeOffset.MaxValue.Ticks 之间。

通过使用 DateTimeOffset(Int64, TimeSpan) 构造函数重载,可以将计时周期数分配给 DateTimeOffset 对象。

 

下面的示例通过估计日期(2008 年 7 月 1 日 1:23:07)中的计时周期数来初始化 DateTimeOffset 对象。然后,它将该日期以及该日期所表示的计时周期数显示到控制台上。

// 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";

posted on 2009-12-07 10:00  小司  阅读(410)  评论(0编辑  收藏  举报