-1

I need a very precise timestamp on my code. I use the first solution of this post:

How to get timestamp of tick precision in .NET / C#?

DateTime _starttime = DateTime.UtcNow;
   Stopwatch _stopwatch = Stopwatch.StartNew();

And then call it later with

DateTime highresDT = _starttime.AddTicks(_stopwatch.Elapsed.Ticks);

But when I want to display it with a MessageBox.Show(highresDT.ToString());

i have only :

30/06/2016 14:59:06

I would like to the millisecond to be displayed. Is It possible?! Thank you.

I'm new on c# it is possible that i totally misunderstood my issue there.

Community
  • 1
  • 1
jsls
  • 251
  • 3
  • 16
  • 2
    This isn't a snarky "go google it" comment - but the amount of information out there is so vast that you can often get the answer faster that way. For example, if you search ".net show milliseconds" the very first result is a page titled "How to: Display Milliseconds in Date and Time Values" which says the same thing as the answers. One benefit of going to the documentation is that you get the answer you're looking for but you also discover other things you can use, and then you remember them later when you need them. – Scott Hannen Jun 30 '16 at 15:16
  • Yes you re right i m feeling stupid..; I thought the solution was like with stopwatch "_stopwatch.Elapsed.TotalMilliseconds.ToString()" (i tried but it didn't work..) and it 's difficult to find a way to adapt your code on google. Thank you anyway !! – jsls Jul 01 '16 at 07:42

4 Answers4

7

You can use MessageBox.Show(highresDT.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));

Sujeet Sinha
  • 2,417
  • 2
  • 18
  • 27
3

This code won't work the way you think it does:

DateTime _starttime = DateTime.UtcNow;
Stopwatch _stopwatch = Stopwatch.StartNew();

At best, your accuracy will be 10-15 ms.

The reason is that UtcNow is only updated once per thread quantum. On Windows on multicore machines, the thread quantum is 15 ms; single core machines every 10 ms.

Stopwatch is very high accuracy and very high precision, so it is possible to do better. I'll illustrate by way of example.

If you have a real stopwatch that you can somehow use and get 1 ms precision and accuracy, can you take an alarm clock that only shows hours and minutes and do any better?

You can. You can measure the passage of time very accurately using the stopwatch. And you can watch the alarm clock to see when it ticks over from 11:14 am to 11:15 am.

For instance, if you observe the following set of events:

  • Read stopwatch: 12000 ms
  • Read clock: 11:14 am
  • Read clock: 11:15 am
  • Read stopwatch: 12002 ms

Then you know in the middle somewhere in there, the time was exactly 11:15:00.0000... and you know that it occurred within a span of time that was only 2 milliseconds, because your stopwatch told you so.

So now you know that stopwatch value 12001 +/- 1 ms == 11:15:00.000000

You can apply this same concept to DateTime.UtcNow() and StopWatch.

If you read the stopwatch and utcnow in a loop, and you see the UtcNow tick over from 11:14:31.030 to 11:14:31.045, then you know that the time was exactly 31.04500000 somewhere in there - and your stopwatch will tell you how much physical time actually passed, and thus you know how much error you have.

antiduh
  • 11,853
  • 4
  • 43
  • 66
  • Did you read the post I link?! (No offense at all it's a real question!!!) because the guy who answer it said this "technique" is a way to avoid the unaccuracy of the thread quantum of the UtcNow. Personnaly i can't say which of you is right.. (i m so new on computer world) But I get what you say but it's quite complicated to implement it to my code (it's already a loop and i need it to be quick very quick and both precise and accurate). I ll try it though :) – jsls Jul 01 '16 at 08:06
  • 1
    @jsls - that guy's wrong. First, UtcNow will return the same value for a period of time of 15 milliseconds, while Stopwatch (via the HighPerformanceCounter API), will return values that are up-to-date. That right there is error, which why if you want more accuracy, you need to do what I describe. Second, thread timing: the OS is constantly swapping threads on and off the CPU, depending on demand and scheduling. It's possible that your thread could be removed from the CPU after calling `DateTime.UtcNow`, but before calling `Stopwatch.StartNew()`. You're not measuring your error. – antiduh Jul 01 '16 at 14:56
1

See this example from Microsoft: https://msdn.microsoft.com/en-us/library/bb882581(v=vs.110).aspx

You need to format it using "hh:mm:ss.fff". fff is format for milisecond.

1

This documentation - Custom Date and Time Format Strings - is useful for reference. You can construct a format string that shows exactly the elements you want. For example,

MessageBox.Show(highresDT.ToString("dd/MM/yyyy HH:mm:ss fff"));

fff indicates that the formatted string should include milliseconds. ('FFF` omits milliseconds if the value is zero.)

Scott Hannen
  • 27,588
  • 3
  • 45
  • 62