使用C#获取以毫秒为单位的时间
我在做一个程序,我需要以毫秒为单位的时间。 到了那个时候,我的意思是一个与自己不相等的数字,总是比第二个数字大1000个数字。 我试过将DateTime.Now转换为TimeSpan并TotalMilliseconds获取TotalMilliseconds …但我听说它并不完全准确。 
有没有更简单的方法来做到这一点?
 使用Stopwatch类。 
提供一组方法和属性,您可以使用这些方法和属性精确测量已用时间。
在这里实现它有一些很好的信息:
性能testing:使用System.Diagnostics.Stopwatch进行精确的运行时间测量
 long milliseconds = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond; 
 这实际上是如何在DateTimeOffset类(.NET Framework 4.6 +,.NET Standard 1.3+)中实现各种Unix转换方法的: 
 long milliseconds = DateTimeOffset.Now.ToUnixTimeMilliseconds(); 
  DateTime.Ticks属性获取表示date和时间的刻度数。 
10,000蜱是一毫秒(每秒10,000,000个蜱)。
 您可以尝试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; } } 
我使用以下类。 我在互联网上发现了一次,推测是最好的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); } } } 
来源不明。
我使用DateTime.Now.TimeOfDay.TotalMilliseconds(当前date),希望它可以帮助你。
 使用System.DateTime.Now.ToUniversalTime() 。 这使您的阅读以已知的基于参考的毫秒格式完全消除了日常变化等。