简体   繁体   English

如何在我的日期时间显示毫秒

[英]How to display millisecond on my Date time

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#? 如何获取.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()); 但是当我想用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. 我是C#的新手,有可能我完全误解了我的问题。

您可以使用MessageBox.Show(highresDT.ToString("MM/dd/yyyy hh:mm:ss.fff tt"));

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. 充其量,您的准确性将是10-15毫秒。

The reason is that UtcNow is only updated once per thread quantum. 原因是每个线程数量UtcNow仅更新一次。 On Windows on multicore machines, the thread quantum is 15 ms; 在多核计算机上的Windows上,线程数量为15毫秒; single core machines every 10 ms. 单核计算机每10毫秒一次。

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? 如果您有可以以某种方式使用并获得1毫秒精度和准确度的真正的秒表,那么您能否采用仅显示小时和分钟的闹钟,并且做得更好?

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. 您可以观看闹钟,看看它何时从上午11:14滴答到上午11:15。

For instance, if you observe the following set of events: 例如,如果您观察到以下事件集:

  • Read stopwatch: 12000 ms 读秒表:12000毫秒
  • Read clock: 11:14 am 阅读时钟:上午11:14
  • Read clock: 11:15 am 阅读时钟:上午11:15
  • Read stopwatch: 12002 ms 读秒表:12002毫秒

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. 然后您知道在中间的某个地方,时间恰好是11:15:00.0000 ...,并且您知道它发生在仅2毫秒的时间内,因为秒表告诉您了。

So now you know that stopwatch value 12001 +/- 1 ms == 11:15:00.000000 所以现在您知道秒表值12001 +/- 1 ms == 11:15:00.000000

You can apply this same concept to DateTime.UtcNow() and StopWatch. 您可以将相同的概念应用于DateTime.UtcNow()和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. 如果您循环阅读秒表和utcnow,并且看到UtcNow从11:14:31.030跳到11:14:31.045,那么您知道那里的时间正好是31.04500000-秒表会告诉您实际花费了多少物理时间,因此您知道自己有多少错误。

See this example from Microsoft: https://msdn.microsoft.com/en-us/library/bb882581(v=vs.110).aspx 请参阅Microsoft的以下示例: https : //msdn.microsoft.com/zh-cn/library/bb882581(v=vs.110).aspx

You need to format it using "hh:mm:ss.fff". 您需要使用“ hh:mm:ss.fff”对其进行格式化。 fff is format for milisecond. fff是毫秒格式。

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")); MessageBox.Show(highresDT.ToString(“ dd / MM / yyyy HH:mm:ss fff”));

fff indicates that the formatted string should include milliseconds. fff表示格式化的字符串应包含毫秒。 ('FFF` omits milliseconds if the value is zero.) (如果该值为零,则“ FFF”将忽略毫秒。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM