开发者

Advance a date_time by seconds + microseconds and display the result?

I'm attempting to print a timestamp to string, where :

Each timestamp is based on开发者_如何转开发 1970.1.1 and incremented by a (long)amount of seconds and a (long) amount of microseconds(always less then 1s total).

Here's what I'm doing :

DateTime TimeStamp = new DateTime(1970, 1, 1);            
TimeSpan span = new TimeSpan(0, 0, 0, (int)Soc, (int)Fracsec.Fracsec);
TimeStamp += span;

I've just noticed that DateTime and TimeSpan use milliseconds instead of microseconds and it is imperative for me that I use microseconds, can anyone tell me how to achieve that?

What I'm trying to do next is to display the result in one operation (hopefully). Any way to do that as well?


Use the Ticks property to achieve microsecond fidelity and DateTime.Format() to display it. See this question for more details. 10 Ticks is equal to 1 microsecond.

There is also a StopWatch class that provides for more accurate measurement of elapsed time.


DateTime is more accurate than just milliseconds - look at the DateTime.Ticks and TimeSpan.Ticks properties. A tick represents 100 nanoseconds (i.e. there are 10 ticks per microsecond; 10,000 per millisecond.) This is defined in TimeSpan.TicksPerMillisecond (a constant).

(One thing to be aware of is that this sort of tick is not the same as the tick in the Stopwatch class, which varies by timer.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜