使用 C# 获取以毫秒为单位的时间
我正在编写一个程序,需要以毫秒为单位获取时间。我所说的时间,是指一个永远不等于自身的数字,并且总是比前一秒大 1000 个数字。我尝试将 DateTime.Now
转换为 TimeSpan
并从中获取 TotalMilliseconds
...但我听说它不是完全准确。
有没有更简单的方法来做到这一点?
I'm making a program in which I need to get the time in milliseconds. By time, I mean a number that is never equal to itself, and is always 1000 numbers bigger than it was a second ago. I've tried converting DateTime.Now
to a TimeSpan
and getting the TotalMilliseconds
from that... but I've heard it isn't perfectly accurate.
Is there an easier way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
这实际上就是
DateTimeOffset
类(.NET Framework 4.6+、.NET Standard 1.3+):This is actually how the various Unix conversion methods are implemented in the
DateTimeOffset
class (.NET Framework 4.6+, .NET Standard 1.3+):使用
Stopwatch
类。这里有一些关于实现它的好信息:
性能测试:精确的运行时间测量使用 System.Diagnostics.Stopwatch
Use the
Stopwatch
class.There is some good info on implementing it here:
Performance Tests: Precise Run Time Measurements with System.Diagnostics.Stopwatch
DateTime.Ticks
属性获取表示日期和时间的刻度数。10,000 滴答声是一毫秒(每秒 10,000,000 滴答声)。
The
DateTime.Ticks
property gets the number of ticks that represent the date and time.10,000 Ticks is a millisecond (10,000,000 ticks per second).
我使用以下课程。我在互联网上发现过一次,被认为是最好的NOW()。
来源不明。
I use the following class. I found it on the Internet once, postulated to be the best NOW().
Source unknown.
您可以尝试
QueryPerformanceCounter
本机方法。请参阅 http://www.pinvoke.net/default.aspx/kernel32/QueryPerformanceCounter .html 了解更多信息。这就是Stopwatch
类所使用的。请参阅如何在.NET /中获取刻度精度的时间戳C#? 了解更多信息。
Stopwatch.GetTimestamp()
提供对此方法的访问:You can try the
QueryPerformanceCounter
native method. See http://www.pinvoke.net/default.aspx/kernel32/QueryPerformanceCounter.html for more information. This is what theStopwatch
class uses.See How to get timestamp of tick precision in .NET / C#? for more information.
Stopwatch.GetTimestamp()
gives access to this method:我使用了 DateTime.Now.TimeOfDay.TotalMilliseconds (当前日期),希望它也能帮助您。
I used DateTime.Now.TimeOfDay.TotalMilliseconds (for current day), hope it helps you out as well.
使用System.DateTime.Now.ToUniversalTime()。这使您的读数采用已知的基于参考的毫秒格式,完全消除了日间变化等。
Use
System.DateTime.Now.ToUniversalTime()
. That puts your reading in a known reference-based millisecond format that totally eliminates day change, etc.使用 Stopwatch 类,我们可以从 System.Diagnostics 实现它。
Using Stopwatch class we can achieve it from
System.Diagnostics
.System.DateTimeOffset.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt")
以 '04/01/2021 04:32:14 的格式获取毫秒.788 PM'https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-display-milliseconds-in-date-and-time -值
System.DateTimeOffset.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt")
to get the millisecond in the format of '04/01/2021 04:32:14.788 PM'https://learn.microsoft.com/en-us/dotnet/standard/base-types/how-to-display-milliseconds-in-date-and-time-values
据我了解您的要求 Environment.TickCount 符合账单。它返回自启动以来的毫秒数,因此它总是增加并可用于计算以毫秒为单位的经过时间。如果您还想要绝对时间,您可以获取当前时间和当前的Environment.TickCount,并根据该时间和新的Environment.TickCount 计算新的绝对时间。
As I understand your requirements Environment.TickCount fits the bill. It returns number of milliseconds since startup, so it always increases and can be used for computing elapsed time in milliseconds. If you want absolute time also, you can get current time and current Environment.TickCount, and compute new absolute time based on that and new Environment.TickCount.