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

发布于 2024-09-29 08:15:53 字数 204 浏览 4 评论 0原文

我正在编写一个程序,需要以毫秒为单位获取时间。我所说的时间,是指一个永远不等于自身的数字,并且总是比前一秒大 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

银河中√捞星星 2024-10-06 08:15:53
long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;

这实际上就是 DateTimeOffset 类(.NET Framework 4.6+、.NET Standard 1.3+):

long milliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds();
long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;

This is actually how the various Unix conversion methods are implemented in the DateTimeOffset class (.NET Framework 4.6+, .NET Standard 1.3+):

long milliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds();
笑看君怀她人 2024-10-06 08:15:53

使用 Stopwatch 类。

提供了一组方法和
您可以使用的属性
准确测量经过的时间。

这里有一些关于实现它的好信息:

性能测试:精确的运行时间测量使用 System.Diagnostics.Stopwatch

Use the Stopwatch class.

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

妞丶爷亲个 2024-10-06 08:15:53

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).

唯憾梦倾城 2024-10-06 08:15:53

我使用以下课程。我在互联网上发现过一次,被认为是最好的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); } }
}

来源不明。

I use the following class. I found it on the Internet once, postulated to be the best 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.

北城挽邺 2024-10-06 08:15:53

您可以尝试QueryPerformanceCounter本机方法。请参阅 http://www.pinvoke.net/default.aspx/kernel32/QueryPerformanceCounter .html 了解更多信息。这就是 Stopwatch 类所使用的。

请参阅如何在.NET /中获取刻度精度的时间戳C#? 了解更多信息。

Stopwatch.GetTimestamp() 提供对此方法的访问:

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

You can try the QueryPerformanceCounter native method. See http://www.pinvoke.net/default.aspx/kernel32/QueryPerformanceCounter.html for more information. This is what the Stopwatch class uses.

See How to get timestamp of tick precision in .NET / C#? for more information.

Stopwatch.GetTimestamp() gives access to this method:

public static long GetTimestamp() {
     if(IsHighResolution) {
         long timestamp = 0;
         SafeNativeMethods.QueryPerformanceCounter(out timestamp);
         return timestamp;
     }
     else {
         return DateTime.UtcNow.Ticks;
     }
 }
兮颜 2024-10-06 08:15:53

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

I used DateTime.Now.TimeOfDay.TotalMilliseconds (for current day), hope it helps you out as well.

夜雨飘雪 2024-10-06 08:15:53

使用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.

终遇你 2024-10-06 08:15:53

使用 Stopwatch 类,我们可以从 System.Diagnostics 实现它。

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

Using Stopwatch class we can achieve it from System.Diagnostics.

Stopwatch stopwatch  = new Stopwatch();
stopwatch.Start();
stopwatch.Stop();
Debug.WriteLine(stopwatch.ElapsedMilliseconds);
爱她像谁 2024-10-06 08:15:53

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

话少心凉 2024-10-06 08:15:53

据我了解您的要求 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文