首页 > 解决方案 > 获得时差

问题描述

您如何使用两种方法找到时间差异。以及如何使用 TimeSpan 调用减法两个方法。

static void Main(string[] args)
{
    int baba;

    var stopWatch = new StopWatch();

    Console.WriteLine("Please enter in any value to get the start time");
    string ti = Console.ReadLine();            
    baba = Convert.ToInt32(ti);
    Console.WriteLine(baba);

    stopWatch.Start();
    Console.WriteLine(Console.Read());

    if(ti != null)
    {
        stopWatch.Stop();
        Console.WriteLine(Console.Read());
    }           

}

标签: c#.netstopwatch

解决方案


请参阅评论中可能的重复链接。另一种计算时间差的方法是从 TimeSpan 中获取:

DateTime time1 = DateTime.Now;
Thread.Sleep(1000);
DateTime time2 = DateTime.Now;
TimeSpan ts = time2 - time1;
Console.WriteLine(ts.TotalMilliseconds);

在这里查看:https ://dotnetfiddle.net/bfHEKY


推荐阅读