简体   繁体   English

使用 C# 以毫秒为单位获取时间

[英]Get time in milliseconds using C#

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.时间,我的意思是一个永远不等于自身的数字,并且总是比一秒前大 1000 个数字。 I've tried converting DateTime.Now to a TimeSpan and getting the TotalMilliseconds from that... but I've heard it isn't perfectly accurate.我已经尝试将DateTime.Now转换为TimeSpanTotalMilliseconds获取TotalMilliseconds ......但我听说它并不完全准确。

Is there an easier way to do this?有没有更简单的方法来做到这一点?

long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;

这实际上是DateTimeOffset类(.NET Framework 4.6+,.NET Standard 1.3+)中各种 Unix 转换方法的实现方式:

long milliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds();

Use the Stopwatch class.使用Stopwatch类。

Provides a set of methods and properties that you can use to accurately measure elapsed time.提供一组可用于准确测量经过时间的方法和属性。

There is some good info on implementing it here:这里有一些关于实施它的好信息:

Performance Tests: Precise Run Time Measurements with System.Diagnostics.Stopwatch 性能测试:使用 System.Diagnostics.Stopwatch 进行精确的运行时间测量

The DateTime.Ticks property gets the number of ticks that represent the date and time. DateTime.Ticks属性获取表示日期和时间的刻度数。

10,000 Ticks is a millisecond (10,000,000 ticks per second). 10,000 滴答是一毫秒(每秒 10,000,000 滴答)。

I use the following class.我使用以下课程。 I found it on the Internet once, postulated to be the best NOW() .我曾经在互联网上找到它,假设是最好的NOW()

/// <summary>Class to get current timestamp with enough precision</summary>
static class CurrentMillis
{
    private static readonly DateTime Jan1St1970 = new DateTime (1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    /// <summary>Get extra long current timestamp</summary>
    public static long Millis { get { return (long)((DateTime.UtcNow - Jan1St1970).TotalMilliseconds); } }
}

Source unknown.来源不明。

You can try the QueryPerformanceCounter native method.您可以尝试QueryPerformanceCounter本机方法。 See http://www.pinvoke.net/default.aspx/kernel32/QueryPerformanceCounter.html for more information.有关详细信息,请参阅http://www.pinvoke.net/default.aspx/kernel32/QueryPerformanceCounter.html This is what the Stopwatch class uses.这是Stopwatch类使用的内容。

See How to get timestamp of tick precision in .NET / C#?请参阅如何在 .NET/C# 中获取刻度精度的时间戳? for more information.了解更多信息。

Stopwatch.GetTimestamp() gives access to this method: Stopwatch.GetTimestamp()可以访问此方法:

public static long GetTimestamp() {
     if(IsHighResolution) {
         long timestamp = 0;
         SafeNativeMethods.QueryPerformanceCounter(out timestamp);
         return timestamp;
     }
     else {
         return DateTime.UtcNow.Ticks;
     }
 }

我使用了 DateTime.Now.TimeOfDay.TotalMilliseconds(当前日期),希望它也能帮助你。

Use System.DateTime.Now.ToUniversalTime() .使用System.DateTime.Now.ToUniversalTime() That puts your reading in a known reference-based millisecond format that totally eliminates day change, etc.这使您的阅读采用已知的基于参考的毫秒格式,完全消除了日期变化等。

Using Stopwatch class we can achieve it from System.Diagnostics .使用 Stopwatch 类,我们可以从System.Diagnostics实现它。

Stopwatch stopwatch  = new Stopwatch();
stopwatch.Start();
stopwatch.Stop();
Debug.WriteLine(stopwatch.ElapsedMilliseconds);

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. System.DateTimeOffset.Now.ToString("MM/dd/yyyy hh:mm:ss.fff tt")以 '04/01/2021 04:32:14 的格式获取毫秒。 788 PM' 788下午'

https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-display-milliseconds-in-date-and-time-values https://docs.microsoft.com/en-us/dotnet/standard/base-types/how-to-display-milliseconds-in-date-and-time-values

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

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